博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
advacing lnux program --4.1.5 Thread Attributes[copy]
阅读量:7055 次
发布时间:2019-06-28

本文共 3325 字,大约阅读时间需要 11 分钟。

Thread attributes provide a mechanism for fine-tuning the behavior of individual      

threads. Recall that pthread_createaccepts an argument that is a pointer to a thread
attribute object. If you pass a null pointer, the default thread attributes are used to
configure the new thread. However, you may create and customize a thread attribute
object to specify other values for the attributes.
To specify customized thread attributes, you must follow these steps:
1. Create a pthread_attr_tobject.The easiest way is simply to declare an auto-matic variable of this type. |声明pthread_attr_tobject变量
2. Call pthread_attr_init, passing a pointer to this object.This initializes the  |调用pthread_attr_init函数
attributes to their default values.
3. Modify the attribute object to contain the desired attribute values.   |根据需要更改属性对象
4. Pass a pointer to the attribute object when calling pthread_create.   |pthread_create中传递属性对象
5. Call pthread_attr_destroyto release the attribute object.The pthread_attr_t  |调用pthread_attr_destroy销毁对象
variable itself is not deallocated; it may be reinitialized with pthread_attr_init.
A single thread attribute object may be used to start several threads. It is not necessary
to keep the thread attribute object around after the threads have been created.

For most GNU/Linux application programming tasks, only one thread attribute is
typically of interest (the other available attributes are primarily for specialty real-time
programming).This attribute is the thread’s detach state. A thread may be created as a
joinable thread (the default) or as a detached thread. A joinable thread, like a process, is not
automatically cleaned up by GNU/Linux when it terminates. Instead, the thread’s exit
state hangs around in the system (kind of like a zombie process) until another thread
calls pthread_jointo obtain its return value. Only then are its resources released. A
detached thread, in contrast, is cleaned up automatically when it terminates. Because a
detached thread is immediately cleaned up, another thread may not synchronize on its
completion by using pthread_joinor obtain its return value.
To set the detach state in a thread attribute object, use pthread_attr_setdetachstate.
The first argument is a pointer to the thread attribute object, and the second is the
desired detach state. Because the joinable state is the default, it is necessary to call this only
to create detached threads; pass PTHREAD_CREATE_DETACHEDas the second argument.
The code in Listing 4.5 creates a detached thread by setting the detach state thread
attribute for the thread.
Listing 4.5 (detached.c) Skeleton Program That Creates a Detached Thread
#include <pthread.h>
void* thread_function (void* thread_arg)
{
/* Do work here... */
}
int main ()
{
pthread_attr_t attr;
pthread_t thread;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
pthread_create (&thread, &attr, &thread_function, NULL);
pthread_attr_destroy (&attr);
/* Do work here... */
/* No need to join the second thread. */
return 0;
}

Even if a thread is created in a joinable state, it may later be turned into a detached
thread.To do this, call pthread_detach. Once a thread is detached, it cannot be made
joinable again.

// 等待线程如何clean  up 呢 ????

转载于:https://www.cnblogs.com/michile/archive/2013/02/03/2890784.html

你可能感兴趣的文章
Unity容器中的对象生存期管理
查看>>
分享:一个多进程并发执行程序ps命令 ls命令
查看>>
Linux iptables 详解
查看>>
路径输入mac下配置NDK开发环境
查看>>
javascript 空字符串和空格的区别
查看>>
猜你喜欢
查看>>
jQuery插件 -- Form表单插件jquery.form.js
查看>>
MFC自绘控件学习总结第二贴
查看>>
FusionCharts 使用经验
查看>>
VMware虚拟化--网络和存储功能简介
查看>>
mingw下python 调用 gcc 无法识别 -mno-cygwin(转)
查看>>
IOS 5新增API介绍及使用
查看>>
文件应用iOS开发-用keychain替代UDID
查看>>
Java回顾之Spring基础
查看>>
Dictionary、KeyValuePair、Hashtable的比较和使用
查看>>
消息电话八卦消息传播时间
查看>>
2千五主机
查看>>
Ehcache学习笔记(二) 根据条件筛选缓存中的数据
查看>>
逻辑数据库设计 - 乱穿马路(多对多关系)
查看>>
Analysis Service Tabular Model #002 Analysis services 的结构:一种产品 两个模型
查看>>