Why is my thread not running in the background?

In the list below, I expect that when I call t.detach() immediately after the line when creating the stream, stream t will run in the background and printf("quit the main function now \n") is called and then main will come out.

 #include <thread> #include <iostream> void hello3(int* i) { for (int j = 0; j < 100; j++) { *i = *i + 1; printf("From new thread %d \n", *i); fflush(stdout); } char c = getchar(); } int main() { int i; i = 0; std::thread t(hello3, &i); t.detach(); printf("quit the main function now \n"); fflush(stdout); return 0; } 

However, from what it displays, this is not so. He is typing

 From new thread 1 From new thread 2 .... From new thread 99 quit the main function now. 

It appears that the main function is waiting for the thread to complete before it executes the printf("quit the main function now \n"); command printf("quit the main function now \n"); and will come out.

Could you explain why this is so? What am I missing here?

+5
source share
4 answers

The problem is that your thread is too fast.

It is capable of printing all 100 lines before main gets a chance to continue.

Try to make the stream slower and you will see the main printf before the stream.

+4
source

This happens based on the planning of your OS. In addition, your flow rate affects output. If you stop the thread (for example, change the value 100 to 500), you will see the message first.

I just executed the code and now "left the main function." first a message appeared:

 quit the main function now From new thread 1 From new thread 2 From new thread 3 From new thread 4 ... 

You are right about detach :

Separates the stream represented by the object from the calling stream, allowing them to execute independently of each other.

but this does not guarantee that the message โ€œexit the main functionโ€ appears first, although this is very likely.

+3
source

It looks like the main function is waiting for the thread to finish before it executes the printf command ("exit the main function now \ n"); and exits.

This is because when you create a thread, it gets scheduled execution, but the order of events on the threads is no longer sequential, ordered, or deterministic. In some runs of your program, the output of hello3 will occur before quit the main function now , in some runs it will be printed later, and in some runs the output will alternate. This is a form of Undefined Behavior, commonly referred to as "Race Status". In most (but not all) cases, hello3 output ends last, because there are some overheads in configuring the stream (it depends on the OS and processor), so it takes several microseconds to properly build a stream and prepare it for execution takes so much time, that the printf operator in your main function has already managed to execute and flush before the thread was ready to run.

If you need clear evidence that everything works at the same time, you should add more work to the main thread before the quit statement, so it is unlikely that the main function will end before the thread is ready to start.

+1
source

There are two main problems in the code:

  • Once the main function completes, you will have undefined behavior because the variable i referenced by the allocated thread no longer exists.

  • As soon as the whole process is completed after the return of the main function, the separable thread will also die.

0
source

Source: https://habr.com/ru/post/1270901/


All Articles