SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3.  
  4. void FunctionPointer()
  5. {
  6.     // create a thread and init
  7.  
  8.     // Action from the main thread
  9.     for (unsigned i = 0; i < 10; i++) {
  10.         std::cout << "Main thread executing" << std::endl;
  11.     }
  12.  
  13.     /// Join the thread
  14.     // The main thread wants to wait for a thread to finish successfully. So, we used join().
  15.     // If the initial main thread didn't wait for the new thread to finish, it would continue to the end of main()
  16.     // and end the program, possibly before the new thread have had a chance to run.
  17. }
  18.  
  19. void Lambda()
  20. {
  21.     // create a thread and init using a lambda
  22.  
  23.     // Action from the main thread
  24.     for (unsigned i = 0; i < 10; i++) {
  25.         std::cout << "Main thread executing" << std::endl;
  26.     }
  27.  
  28.     /// Join the thread
  29. }
  30.  
  31. void CreateThread()
  32. {
  33.     FunctionPointer();
  34.  
  35.     Lambda();
  36. }
  37.  
  38.  
  39. void TheJoin()
  40. {
  41.     /// Tip
  42.     //  Never call join() or detach() on std::thread object with no associated executing thread
  43.  
  44.     // create a vector of threads
  45.  
  46.     // int the vector and output the thread id in each one
  47.  
  48.     // join all threads (maybe using mem_fn)
  49. }
  50.  
  51. void TheDetach()
  52. {
  53.     /// Tip
  54.     // Never forget to call either join or detach on a std::thread object with associated executing thread
  55.  
  56.     // create a vector of threads
  57.    
  58.     // int the vector and output the thread id in each one
  59.  
  60.     // detach all threads (maybe using mem_fn)
  61. }
  62.  
  63. void JoinAndDetach()
  64. {
  65.     TheJoin();
  66.  
  67.     TheDetach();
  68. }
  69.  
  70.  
  71. void threadOne(int x, char s)
  72. {
  73.     std::cout << x << s << std::endl;
  74. }
  75.  
  76. void SimpleArguments()
  77. {
  78.     // Init a thread variable with threadOne method
  79.     // Check if joinable and join
  80. }
  81.  
  82. void HowNotTo()
  83. {
  84.     // This is an example of how not to pass a pointer to a thread
  85.  
  86.     // 1. Create a thread
  87.     // 2. Inside that thread create a pointer
  88.     // 3. Inside that thread create a new thread and pass the pointer to the new created thread
  89.     // 4. Simulate some heavy computation (with sleep) in the second thread
  90.     // 5. Modify the value of the pointer in the second thread
  91.     // 6. In the first thread also modify the value of the pointer
  92.     // 7. delete the pointer
  93.     // 8. Join the second thread in the first thread
  94.     // 9. Join the first thread
  95.     // 10. What is going on ?
  96. }
  97.  
  98. void HowNotTo2()
  99. {
  100.     // How not to pass references to thread
  101.     //
  102.  
  103.     //
  104.     // Even if threadCallback accepts arguments as reference but still changes done it are not visible outside the thread.
  105.     // Its because x in the thread function threadCallback is reference to the temporary value copied at the new thread’s stack.
  106.  
  107.     // 1. Create a new thread and pass a const referece to it
  108.     // 2. Modify the const reference variale inside that thread
  109.     // 3. Join the thread
  110.     // 4. Do some logging inside and outside the thread, what values are you expected to get?
  111. }
  112.  
  113. void HowToPassRefArg()
  114. {
  115.     // 1. Create a new thread and pass a const referece to it
  116.     // 2. Modify the const reference variale inside that thread
  117.     // 3. Join the thread
  118.     // 4. Do some logging inside and outside the thread, what values are you expected to get?
  119. }
  120.  
  121. void ThreadArguments()
  122. {
  123.     SimpleArguments();
  124.  
  125.     HowNotTo();
  126.  
  127.     HowNotTo2();
  128.  
  129.     HowToPassRefArg();
  130. }
  131.  
  132. int main()
  133. {
  134.     CreateThread();
  135.  
  136.     JoinAndDetach();
  137.  
  138.     ThreadArguments();
  139.  
  140.     std::cin.get();
  141.  
  142.     return 0;
  143. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top