fork(3) download
  1. #include <iostream>
  2. #include <thread>
  3.  
  4. class f
  5. {
  6. public:
  7. f(int i = 0, std::string s = "") : _i(i), _s(s) { }
  8. void operator()() const
  9. {
  10. for(int i = 0; i < _i; ++i)
  11. std::cout << _s << std::endl;
  12. }
  13. int _i;
  14. std::string _s;
  15. };
  16.  
  17.  
  18. int main()
  19. {
  20. //std::thread t1(f()); // Most vexing parse (Scott Meyers: Effective STL)
  21. std::thread t2((f(3, "Hello"))); // OK
  22. //t1.join(); // prog.cpp:23:5: error: request for member 'join' in 't1',
  23. // which is of non-class type 'std::thread(f (*)())'
  24. t2.join();
  25. return 0;
  26. }
Success #stdin #stdout 0s 4284KB
stdin
Standard input is empty
stdout
Hello
Hello
Hello