Advertisement
P22DX

multi_thread.cpp

Jun 11th, 2020
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. /**
  2.  * sync multi threading with c++
  3.  * flag : -pthread -std=c++0x
  4.  */
  5. #include <thread>
  6. #include <iostream>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9.  
  10. class MyClass {
  11.     public:
  12.     auto doStuff()->void
  13.     {
  14.         std::cout << "doing stuff so long" << std::endl;
  15.         sleep(3);
  16.     }
  17.     auto doAnotherStuff()->void
  18.     {
  19.         std::cout << "doing another stuff" << std::endl;
  20.     }
  21. };
  22.  
  23. int main()
  24. {
  25.     sleep(3); // give a time to open task manager
  26.     MyClass app;
  27.  
  28.     std::thread worker1(&MyClass::doStuff, app);
  29.     std::thread worker2(&MyClass::doAnotherStuff, app);
  30.  
  31.     if(worker1.joinable()) worker1.join();
  32.     if(worker2.joinable()) worker2.join();
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement