#include <iostream> #include <vector> #include <thread> #include "thread_safe_random.hpp" // auxiliary function for the construction of std::thread void fetch_random_number(double & x) { x = RANDOM(); } int main() { int max = 10; int my_seed = 235; Seed(my_seed); // a vector that will contain the threads std::vector<std::thread> threads(max); // a vector to store the results std::vector<double> xs(max); // create threads of execution that fetch a random number for ( int i = 0; i < max; ++i ) { // NB: xs[i] has to be wrapped in std::ref to pass a reference threads[i] = std::thread(fetch_random_number, std::ref(xs[i])); } // synchonize and print the results for ( int i = 0; i < max; ++i ) { threads[i].join(); // when join() returns, xs[i] contains a fresh random number std::cout << xs[i] << std::endl; } return 0; }