Detached vs. Joinable POSIX threads

前端 未结 2 1451
面向向阳花
面向向阳花 2020-11-27 10:33

I\'ve been using the pthread library for creating & joining threads in C.

  1. When should I create a thread as detached, right from the outset?

2条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 11:21

    1. Create a detached thread when you know you won't want to wait for it with pthread_join(). The only performance benefit is that when a detached thread terminates, its resources can be released immediately instead of having to wait for the thread to be joined before the resources can be released.

    2. It is 'legal' not to join a joinable thread; but it is not usually advisable because (as previously noted) the resources won't be released until the thread is joined, so they'll remain tied up indefinitely (until the program exits) if you don't join it.

提交回复
热议问题