How to fix thread joining issue

Feb 18, 2018 at 1:55am
My code hangs up when trying to join the second thread. I looked online, but it seems it is similar to https://stackoverflow.com/questions/33431445/how-do-i-use-while-true-in-threads.
Pseudo code since I do not have the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
main()
{
   Chain1 generator;
   Chain2 preprocessor;
   Chain3 processor;

   // to communicate to the parent for data transfer
   processsor.makeChild(&generator);
   processor.makeChild(&preprocessor);

   // initialize the threads
   generator.init();
   preprocessor.init();
   processor.init();

   // join the threads
   generator.start();
   preprocessor.start(); // issue here hangs up here trying to join
   processor.start();
}

class Chain1 //or Chain2 or Chain3...
{
     init()
     {
         // stored as a class member
         chain_thread = std::thread(&Chain1::Dofunction, this); // or std::thread(&Chain2::Dofunction, this); // or std::thread(&Chain3::Dofunction, this);
     }
     start()
     {
        chain_thread.join();   
     }
     Dofunction()
     {
         while(true)
         {
             //...  different for each Chain1, Chain2, Chain3
         }
     }
Feb 18, 2018 at 9:14am
join() waits until the thread has finished. If the thread runs an infinite loop it means it will never finish so join() will wait forever.
Topic archived. No new replies allowed.