Home > Back-end >  Under Linux, c using STD: : thread, join an exception is thrown
Under Linux, c using STD: : thread, join an exception is thrown

Time:10-12


Project architecture used in the tertiary process model, for the sake of the fork after the code is not succession process of worker threads at the next higher level, so every time before the fork all create a child thread to the fork.
And found the following problems:
1. The grandson in the process of using STD: : thread to create a worker thread work_thread, throw an exception when they Join.
2. If the son of the fork in the process of the code is not placed in the child thread, directly in the main thread of the fork will not throw an exception
3. If the grandson process, open a worker thread work_thread before, create a empty_thread (do nothing) threads, in work_thread. The join () before empty_thread. The join () also won't appear abnormal, but if the join function called empty_thread before work_thread will throw an exception
consult everybody a great god why :
Recurring problems test code is as follows:
 
#include
#include
#include
#include
using namespace std;
Int main (int arg c, char * * argv)
{
STD: : thread t ([] () {
Int l2pid=fork ();
If (l2pid & gt; 0)
Waitpid (l2pid, NULL, 0);
Else if (l2pid==0)
{
STD: : thread t2 ([] () {
Int l3pid=fork ();
If (l3pid & gt; 0)
Waitpid (l3pid, NULL, 0);
Else if (l3pid==0) {
//STD: : thread empty_thread ([] () {
//});
STD: : thread work_thread ([] () {
for(int i=0; I & lt; 5; I++) {
STD: : cout & lt; <"Working... "& lt; STD: : this_thread: : sleep_for (STD: : chrono: : seconds (1));
}
});
If (work_thread joinable ())
Work_thread. The join ();
//if (empty_thread joinable ())
//empty_thread. The join ();
STD: : cout & lt; <"L3 Process Exit!" }
});
If (t2) joinable ())
T2. The join ();
STD: : cout & lt; <"L2 Process Exit!" }
});
If (t.j oinable ())
T.j oin ();
STD: : cout & lt; <"Main Process Exit!" return 0;
}

  • Related