fork(1) download
  1. #include <iostream>
  2. #include <thread>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. class markov_chain
  7. {
  8. public:
  9. unsigned int length{0}, acceptance{0};
  10.  
  11. markov_chain(unsigned int l) { length = l; }
  12. ~markov_chain() {}
  13.  
  14. void sample(int acc);
  15. };
  16.  
  17. void markov_chain::sample(int acc)
  18. {
  19. acceptance = acc;
  20. std::cout << length << ' ' << acceptance << std::endl;
  21. }
  22.  
  23. int main()
  24. {
  25. int number_of_threads{3};
  26. int number_of_samples{1000};
  27.  
  28. std::vector<markov_chain> chains;
  29. std::vector<std::thread> workers;
  30.  
  31. chains.reserve(number_of_threads);
  32.  
  33. for (int i = 0; i < number_of_threads; ++i) {
  34. chains.push_back(markov_chain(number_of_samples));
  35. workers.push_back(std::thread(&markov_chain::sample, &chains[i], 99));
  36. }
  37.  
  38. for(auto &t : workers) {
  39. t.join();
  40. }
  41.  
  42. for (auto &c : chains) {
  43. std::cout << c.length << ' ' << c.acceptance << std::endl;
  44. }
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 5364KB
stdin
Standard input is empty
stdout
1000 99
1000 99
1000 99
1000 99
1000 99
1000 99