-1

I have a doubt about threading basics. If I launch a new thread T1, the main thread has to wait for T1 to join or can continue its execution without waiting?.

If it has to wait, can I make both the main and the new threads run asynchronously?.

assylias
  • 321,522
  • 82
  • 660
  • 783
Kio Marv
  • 337
  • 2
  • 3
  • 8

1 Answers1

2

If you does not call join() both thread executes concurrently. By default threads runs asynchronously. join() blocks the calling thread until a thread terminates.


I would like to add to make thread execute synchronised, concurrency control mechanisms are provided.

One can explore about POSIX Threads Programming

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • Hi!. So, if I put in my code `t1.join()` the main thread will have to wait for it, right?. Thanks!. – Kio Marv Dec 07 '12 at 10:51
  • 1
    yes, if in main thread you calls `ti.join()` then main thread will waits. – Grijesh Chauhan Dec 07 '12 at 10:53
  • Another little point, if I don't use `join()`, once the thread has finished the computation, it will reunite with the main thread on its own, isn't it?. I mean, it will destroy itself once is done. And what if the main thread finished before the other one?. Then this will end in undefined behaviour, right?. Thanks. – Kio Marv Dec 07 '12 at 11:08
  • If main thread dies then all its child thread will also terminates. – Grijesh Chauhan Dec 07 '12 at 12:15
  • @KioMarv : CAUTION : All my answer is in context of C and by-default behaviour of thread..In python and Java there is concept of Demon thread also those are eligible to be alive even after main thread dies. There is a important role of virtual machine in thread execution. – Grijesh Chauhan Dec 07 '12 at 12:46
  • You can learn more about thread termination issues [HERE](http://stackoverflow.com/questions/1725429/thread-termination-issue-c-programming) – Grijesh Chauhan Dec 07 '12 at 12:48
  • 1
    Don't worry!. I'm interested in C++ only. ust forgot to set that tag :D And thanks for the link!. – Kio Marv Dec 07 '12 at 13:05