Say I have $n$ threads executing in a parallel region and one of them leaves the parallel region when others are still continuing to execute in the parallel region. Why this is a $BAD$ idea? For example you can consider this program.


10 sum(id)=next(id)

30 result(id)=sum(id)*9.8
   if(converged(result(id)) goto 20
   go to 10

   if(not_FINISHED) goto 30

20 print *,id

In this code we are branching out of the parallel region. I have read that its a $BAD$ idea to do so, but I have not got the reason. So it will be very helpful if someone can shed some light on this.

  • 3
    $\begingroup$ Please don't use mathmode as a way to highlight words; this is a typographic nightmare. $\endgroup$ – Watercrystal Oct 28 '20 at 9:35
  • $\begingroup$ Does stackoverflow.com/a/17394580/7092484 answer your question? $\endgroup$ – Throckmorton Oct 28 '20 at 18:00
  • $\begingroup$ @Throckmorton I have seen that answer, but I am looking for a more detailed explanation. That answer only gives an overview. $\endgroup$ – Turing101 Oct 28 '20 at 20:55
  • $\begingroup$ Please include in the question what research you've done and what you've found and explain why it didn't meet your needs. If you already know of an answer and you didn't understand it, please link to that answer and explain what you did understand and what you didn't understand. Otherwise, we risk repeating something that you already know isn't working for you. Thank you. $\endgroup$ – D.W. Oct 31 '20 at 19:28

OS functions are called to create and join a thread.

The main thread waits for all created thread to return (join). When a thread jump out in middle, the main thread will wait infinitely. In the example you have given one of the spawned thread jumps to a location in main thread. It will proceed and the program will eventually end. Any thread can exit/close the program. It may be the case that other spawned threads are in different stages of their execution and since parent (main) thread has exited, they will terminate inflight as well.

A lot of resources allocated to the abruptly terminated threads by OS, will waste. A properly terminated thread releases all the resources allocated to it properly, improperly terminated threads won't.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.