当前位置: 技术问答>linux和unix
将一个线程定义为Detached意味着什么?
来源: 互联网 发布时间:2015-04-27
本文导语: pthread_mutex_t mutex; void *tick(void*) { ………… pthread_detach(pthread_self()); for(;;) { pthread_mutex_lock(&mutex); …………………… pthread_mutex_unlock(&mutex); } ...
pthread_mutex_t mutex;
void *tick(void*)
{
…………
pthread_detach(pthread_self());
for(;;)
{
pthread_mutex_lock(&mutex);
……………………
pthread_mutex_unlock(&mutex);
}
…………
}
是不是就不用调用pthread_exit();还意味着什么?
void *tick(void*)
{
…………
pthread_detach(pthread_self());
for(;;)
{
pthread_mutex_lock(&mutex);
……………………
pthread_mutex_unlock(&mutex);
}
…………
}
是不是就不用调用pthread_exit();还意味着什么?
|
由于线程也占用资源,如果你不设置成为detach状态,那么当你的线程推出后,你必须执行
pthread_join调用才能释放这些被占用的资源,如果设置成detach状态,线程再推出后将自动释放自己占用的资源
这些占用的资源不包括使用malloc分配的内存和ipc资源
pthread_join调用才能释放这些被占用的资源,如果设置成detach状态,线程再推出后将自动释放自己占用的资源
这些占用的资源不包括使用malloc分配的内存和ipc资源
|
The pthread_detach() function is used to indicate to the
implementation that storage for the thread thread can be
reclaimed when that thread terminates. In other words,
pthread_detach() dynamically resets the detachstate attri-
bute of the thread to PTHREAD_CREATE_DETACHED. After a suc-
cessful call to this function, it would not be necessary to
reclaim the thread using pthread_join(). See
pthread_join(3THR). If thread has not terminated,
pthread_detach() will not cause it to terminate. The effect
of multiple pthread_detach() calls on the same target thread
is unspecified.
implementation that storage for the thread thread can be
reclaimed when that thread terminates. In other words,
pthread_detach() dynamically resets the detachstate attri-
bute of the thread to PTHREAD_CREATE_DETACHED. After a suc-
cessful call to this function, it would not be necessary to
reclaim the thread using pthread_join(). See
pthread_join(3THR). If thread has not terminated,
pthread_detach() will not cause it to terminate. The effect
of multiple pthread_detach() calls on the same target thread
is unspecified.
|
每个线程有自己的数据结构记录运行状态,这些都需要释放。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。