Advertisement
framp

Waiter c++0x

Apr 4th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. //g++ fun.cpp -std=c++11 -pthread && ./a.out
  2. #include <iostream>
  3. #include <chrono>
  4. #include <thread>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. class Waiter{
  9. private:
  10.     vector<thread> ts;
  11. public:
  12.     void add(function<void(void)> f){
  13.         ts.push_back(thread(f));
  14.     }
  15.    
  16.     ~Waiter(){
  17.         for (auto &t: ts){
  18.             t.join();
  19.         }
  20.     }
  21. };
  22.  
  23. int main()
  24. {
  25.     Waiter w;
  26.     cout << "START\n";
  27.     w.add([] () {  cout << "1 thread\n"; this_thread::sleep_for(chrono::seconds(5)); cout << "1 thread over\n"; });
  28.     w.add([] () {  cout << "2 thread\n"; this_thread::sleep_for(chrono::seconds(3)); cout << "2 thread over\n"; });
  29.     cout << "END\n";
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement