Does thread.join ensure that execution is completed | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Does thread.join ensure that execution is completed

Hi I have an idea that we should join the thread so that it asks main thread to wait for it. What I am not sure is sequence of the executions.. Once I write a statement that threadobj.join() Does it block the next statement until the entire thread function is not over ? I mean what should be done when I want my thread function to get completed before some statement are executed?

30th Oct 2022, 12:47 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Answers
+ 2
Your assumption is correct. https://en.cppreference.com/w/cpp/thread/thread/join std::thread::join "Blocks the current thread until the thread identified by *this finishes its execution."
30th Oct 2022, 2:59 PM
Tibor Santa
Tibor Santa - avatar
+ 3
I found this article has very good explanations for C++ https://stackoverflow.com/questions/11004273/what-is-stdpromise My understanding: A future is an abstraction to encapsulate a value that is not immediately available, it could be calculated on a separate thread or even on a remote machine. A promise is the mechanism to carry this value to the thread where it would be used. This is my Clojure essay of this topic, not sure if helpful for you, but these concepts are very similar in all languages. https://code.sololearn.com/Waa05F64uyQf/?ref=app
30th Oct 2022, 4:23 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Thank you
30th Oct 2022, 6:03 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Sounds good... Now whats the purpose of future and promise ? We have blocking by .join() method which ensures that thread execution is done Now , we can just pass argument as ref to get value updated from thread function... Whats the need of future and promise ? Do they accomplish something which ref argument and thread join cant achieve ?
30th Oct 2022, 3:31 PM
Ketan Lalcheta
Ketan Lalcheta - avatar