✅ Thread and Threading
i have asked this question on the #help-0 an here is the link https://discord.com/channels/143867839282020352/169726586931773440/1269020486952816671 according to conversation with @TeBeCo i
have changed the code to be like below by question is now
T1.join() will block the main thread till it resolves , and then the T2 will block the main thread and the T1 Thread till it resolves is that correct
14 Replies
steven preadly
can any one give me an opinon about this example to explian the
thread.join()
method
Quoted by
<@1190732907942260737> from #help-0 (click here)
React with ❌ to remove this embed.
T2 will not block T1, because T2 will only be created after T1 exits
Though if the goal is to show case how
join
ing a thread works a simpler example would be better like spawning a sinle child thread that waits for few seconds, and in the main thread you have stopwatch that starts before Join
and ends after it, then priting out how many seconds have elapsed .if you dont mind can you give me a written code example
the result of the above code is
also i get another example from the docs , i the below case the blocking will be for the thread 1 till thread 2 finish it work , is that correct
No, main thread is not
join
ing thread 1
Thread 1 however is joining thread2
, though it might not even join it because it'll only join
thread 2 once thread 2 is started, which might not be the case
Imo something simple like that would be good
the child will block the main correct ?
Yup, main will be blocked for 5 seconds
i am confused in this part main thread are joining The child thread or the child thread joined to the main thread
i wouldn't call them child threads
there is no thread hierarchy
True, that's bad naming on my part 😅
Main thread is joining child thread, or a better name would be
thread1
Main thread is blocked until the other thread exitswhen you call
Join()
then whatever thread you called Join()
from will block until the thread that you called Join()
on exitsThis graphic might help
https://miro.medium.com/v2/resize:fit:2000/1*F2OBjK1UVNaSerAe9dynGg.png
this is an example that are identical to the on i made in above link but it is smaller i made this to understand the Join behavior sorry for asking o much question and i know that the example above is simpler
in this example main thread joined T1 ok and in the threadone method i made another thread on T2 so T1 Will join T2 , T2 will block T1 and T1 Will block main is that correct ?
i have read this object
Yes, that's correct
Main thread
started and joined Thread1
, Main thread
is now blocked until Thread1
exits
Thread1
started and joined Thread2
, Thread1
is now blocked until Thread2
exits
Thread2
started and done executing, which means Thread1
is now free to continue executing which causes it to exit, after that Main Thread
is finally free to continue exectuing as wellI know that its a little bit of a complicated example but this for me to understand
Thank you @Kouhai and @Jimmacle