posix多线程有感--线程高级编程(线程属性pthread_attr_t)
来源:互联网 发布:阿里云资源短信包 编辑:程序博客网 时间:2024/04/27 19:10
1.线程属性
名称::
pthread_attr_init/pthread_attr_destroy
功能:
对线程属性初始化/去除初始化
头文件:
#include<pthread.h>
函数原形:
int pthread_attr_init(pthread_attr_t* attr);
int pthread_attr_destroy(pthread_attr_t* attr);
参数:
Attr
返回值:
若成功返回0,若失败返回-1。
调用pthread_attr_init之后,pthread_t结构所包含的内容就是操作系统实现支持的线程所有属性的默认值。
线程属性结构如下:
typedef struct
{
}pthread_attr_t;
每个个属性都对应一些函数对其查看或修改。下面我们分别介绍。
二、线程的分离状态
而分离线程不是这样子的,它没有被其他的线程所等待,自己运行结束了,线程也就终止了,马上释放系统资源。程序员应该根据自己的需要,选择适当的分离状态。所以如果我们在创建线程时就知道不需要了解线程的终止状态,则可以pthread_attr_t结构中的detachstate线程属性,让线程以分离状态启动。
名称::
pthread_attr_getdetachstate/pthread_attr_setdetachstate
功能:
获取/修改线程的分离状态属性
头文件:
#include<pthread.h>
函数原形:
int pthread_attr_getdetachstate(const pthread_attr_t *attr,int *detachstate);
int pthread_attr_setdetachstate(pthread_attr_t *attr,int detachstate);
参数:
Attr
Detachstate
返回值:
若成功返回0,若失败返回-1。
可以使用pthread_attr_setdetachstate函数把线程属性detachstate设置为下面的两个合法值之一:设置为PTHREAD_CREATE_DETACHED,以分离状态启动线程;或者设置为PTHREAD_CREATE_JOINABLE,正常启动线程。可以使用pthread_attr_getdetachstate函数获取当前的datachstate线程属性。
以分离状态创建线程
#iinclude<pthread.h>
void *child_thread(void *arg)
{
printf(“child thread run!\n”);
}
int main(int argc,char *argv[ ])
{
}
- posix多线程有感--线程高级编程(线程属性pthread_attr_t)
- posix多线程有感--线程高级编程(线程属性pthread_attr_t)---实时调度(代码)
- posix多线程有感--线程高级编程(线程属性pthread_attr_t)---实时调度
- posix多线程有感--线程高级编程(互斥量属性)
- posix多线程有感--线程高级编程(条件变量属性)
- posix多线程有感--线程高级编程(线程属性函数总结)
- posix多线程有感--线程高级编程(线程内存模式)
- posix多线程有感--线程高级编程(线程堆栈)
- Linux 多线程编程( POSIX )( 二 )----->pthread_attr_t 线程属性
- Linux 多线程编程( POSIX )( 二 )----->pthread_attr_t 线程属性
- Linux 多线程编程( POSIX )( 二 )----->pthread_attr_t 线程属性
- posix多线程有感--线程高级编程(条件变量)
- posix多线程有感--线程高级编程(pthread_once)
- posix多线程有感--线程高级编程(pthread_key_t)
- posix多线程有感--线程高级编程(进程的优先级)
- posix多线程有感--线程高级编程(优先级有关)
- posix多线程有感--线程高级编程(条件变量)
- posix多线程有感--线程高级编程(互斥量属性)(代码)
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- 窗口类,窗口类对象与窗口
- Cocos2d-x 在屏幕上渲染菜单并添加消息响应
- boost之date/time学习
- 常用命令
- posix多线程有感--线程高级编程(线程属性pthread_attr_t)
- native server smack 说明 结构
- jquery实现全选、反选以及删除、添加等功能
- 创建文件夹,存储图片,复制文件类
- Qt 进度条实现
- jquery_api_1.9 中文手册
- Debian Wheezy Gnome3 使用和配置
- 设计模式___建造者模式
- Python通过thrift连接Hbase(详细配置步骤)