| 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run } |
| 19 | // { dg-options "-pthread" } |
| 20 | // { dg-require-effective-target c++11 } |
| 21 | // { dg-require-effective-target pthread } |
| 22 | // { dg-require-gthreads "" } |
| 23 | |
| 24 | #include <random> |
| 25 | #include <memory> |
| 26 | #include <thread> |
| 27 | #include <cstdio> |
| 28 | #include <testsuite_random.h> |
| 29 | |
| 30 | using __gnu_test::random_device_available; |
| 31 | |
| 32 | void read_random_device(const char* token, int iterations) |
| 33 | { |
| 34 | std::random_device dev(token); |
| 35 | for (int i = 0; i != iterations; ++i) |
| 36 | (void) dev(); |
| 37 | } |
| 38 | |
| 39 | int main() { |
| 40 | std::thread workers[8]; |
| 41 | |
| 42 | // N.B. don't test /dev/random as it might block, and /dev/urandom |
| 43 | // "can incur an appreciable delay when requesting large amounts of data". |
| 44 | for (const char* dev : { "default", "rdrand", "rdseed", "rand_s" }) |
| 45 | { |
| 46 | if (random_device_available(dev)) |
| 47 | { |
| 48 | for (auto& w : workers) |
| 49 | w = std::thread{read_random_device, dev, 1000}; |
| 50 | for (auto& w : workers) |
| 51 | w.join(); |
| 52 | } |
| 53 | else |
| 54 | std::printf("random_device(\"%s\") not available\n", dev); |
| 55 | } |
| 56 | } |