独立与可连接POSIX线程 [英] Detached vs. Joinable POSIX threads

查看:77
本文介绍了独立与可连接POSIX线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用pthread库,用于创建和放大器;用C连接螺纹。


  1. 当我应该创建作为独立线程,从一开始就?它是否提供任何性能上的优势对一个可连接线程?


  2. 是合法的没有做在pthread_join()上的可连接(默认)的线程?还是应该这样的线程总是使用前了pthread_exit()ING的分离()函数?



解决方案

  1. 创建分离线程,当你知道你不会希望与来等待它在pthread_join()。唯一的性能优点是,当一个分离的线程终止,它的资源可以被立即释放,而不必等待线程要接合的资源可以被释放之前


  2. 这是合法不参加合并线程;但它不是通常建议,因为(previously说明)的资源不会被释放,直到该线程被加入,所以他们会无限期地捆绑起来(直到退出程序),如果你不加入。


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? Does it offer any performance advantage vs. a joinable thread?

  2. Is it legal to not do a pthread_join() on a joinable (by default) thread? Or should such a thread always use the detach() function before pthread_exit()ing?

解决方案

  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.

这篇关于独立与可连接POSIX线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆