Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //g++ fun.cpp -std=c++11 -pthread && ./a.out
- #include <iostream>
- #include <chrono>
- #include <thread>
- #include <vector>
- using namespace std;
- class Waiter{
- private:
- vector<thread> ts;
- public:
- void add(function<void(void)> f){
- ts.push_back(thread(f));
- }
- ~Waiter(){
- for (auto &t: ts){
- t.join();
- }
- }
- };
- int main()
- {
- Waiter w;
- cout << "START\n";
- w.add([] () { cout << "1 thread\n"; this_thread::sleep_for(chrono::seconds(5)); cout << "1 thread over\n"; });
- w.add([] () { cout << "2 thread\n"; this_thread::sleep_for(chrono::seconds(3)); cout << "2 thread over\n"; });
- cout << "END\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement