开发者

Benefits of Thread Joining

开发者 https://www.devze.com 2022-12-14 16:46 出处:网络
What are the benefits of thread joining ? If the point is to join a thread to stop a thread A unt开发者_开发技巧il thread B is finished executing, for instance (B.join()) why not use a global variable

What are the benefits of thread joining ? If the point is to join a thread to stop a thread A unt开发者_开发技巧il thread B is finished executing, for instance (B.join()) why not use a global variable to do this?


Because join() allows A to go to sleep and not consume CPU time until B ends, and it also ensures that B has completely ended before continuing with A. In general it's just far more efficient. It also avoids the hassle of creating an additional variable.


Another reason you want to call pthread_join() or pthread_detach() to thread that was created with flag PTHREAD_CREATE_JOINABLE is: the system can reclaim all resources associated with the thread after thread termination. Failure to join to or detach joinable threads causes memory and other resource leaks until the process ends.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号