Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Should you always put join() after launching a thread?

I wonder if there is any advantage putting join() not immediately after launching a thread?

std::thread t(func);

// some code ...

t.join();

does it give you any advantage or it’s always preferable to use it after a thread launch?

std::thread t(func);
t.join();

// some code ...

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you use join() right after starting the new thread then it will block (wait) execution on the join() call until the new thread is finished running (defeating the whole purpose of the parallelization you get from starting the new thread). Therefore, if you want to execute "some code" on the main thread while thread t is executing, join() should come after "some code".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading