-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
detach() has no output at all #55
Comments
On 2024-10-17 08:32, ggankhuy wrote:
I extended the example in the page 18 as follows. It compiles and runs
fine and I can see constructor being called looking at terminal
output. But when detach() called, I cant see, any output from the
operator() (). I thought operator() will be invoked when my_func is
passed my_thread() initialization:
std:thread my_thread(my_func) and detach() allows to run in the
background even after main() quits.
But I dont see any output with detach() is called before exising
main().
However, if I replace detach() with join(), it works and I get the
loop executing as main() blocks until operator()() finish executing.
Can you explain what I did wrong here?
After calling detach you exit the program. Since the code does not wait
for the thread to finish, then it is not guaranteed to have run any
particular part of the code on that thread. e.g. the OS might not have
scheduled your thread to even *start* yet.
This is why, if you add a delay after detach then you see output: now
your code has run --- it's still not guaranteed, but if there is nothing
else for your system to do then it will run your code. Similarly, if you
explicitly wait for it with join then it is guaranteed to run.
Cheers,
Anthony
|
Yeah for join(), it is pretty much clear, for detach() hmm, in the book, it says even after main() exits, if detached, thread should be owned by C++ runtime and if I understand correctly, it says "detached threads truly run in the background, ownership and control are passsed over to the C++ Runtime Lib", I do see there is some ambiguity in that it does not tell whether it should continue to run after main() exists, but if it becomes completely detached would not that mean it should continue to run after main() exit? Or does it simply stating, while main() is running, it will be independent and run as daemon? |
I might have found official explanation for this, but in different C book: |
it seems join will be just remedy for main not waiting for other to finish if detach is called. Interesting note from same book, it also says pthread_exit() can be used to allow the process to continue untill all threads are terminated. But c++ thread does not seem to offer such API https://en.cppreference.com/w/cpp/thread/thread perhaps jsut use join then. |
I extended the example in the page 18 as follows. It compiles and runs fine and I can see constructor being called looking at terminal output. But when detach() called, I cant see, any output from the operator() (). I thought operator() will be invoked when my_func is passed my_thread() initialization:
std:thread my_thread(my_func) and detach() allows to run in the background even after main() quits.
But I dont see any output with detach() is called before exising main().
However, if I replace detach() with join(), it works and I get the loop executing as main() blocks until operator()() finish executing.
Can you explain what I did wrong here?
Ifound similar example on the internet and by the same manner, if I delay the exiting of main() (using chrono API to wait one sec (whcih is a very long time!), followed by detach() now I do see outputs now. So lack of output I mentioned above is main() exits without waiting.
The text was updated successfully, but these errors were encountered: