线程属性介绍
来源:互联网 发布:centos下关闭selinux 编辑:程序博客网 时间:2024/06/15 15:09
pthread_attr_init线程属性
一.线程属性
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。
线程属性结构如下:
typedef struct
{
}pthread_attr_t;
每个个属性都对应一些函数对其查看或修改。下面我们分别介绍。
二、线程的分离状态
而分离线程不是这样子的,它没有被其他的线程所等待,自己运行结束了,线程也就终止了,马上释放系统资源。程序员应该根据自己的需要,选择适当的分离状态。所以如果我们在创建线程时就知道不需要了解线程的终止状态,则可以pthread_attr_t结构中的detachstate线程属性,让线程以分离状态启动。
2.
名称::
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[ ])
{
}
三、线程的继承性
3.
名称::
pthread_attr_getinheritsched
pthread_attr_setinheritsched
功能:
获得/设置线程的继承性
头文件:
#include <pthread.h>
函数原形:
int pthread_attr_getinheritsched(const pthread_attr_t *attr,int *inheritsched);
int pthread_attr_setinheritsched(pthread_attr_t *attr,int inheritsched);
参数:
attr
inheritsched
返回值:
若成功返回0,若失败返回-1。
四、线程的调度策略
4.
名称::
pthread_attr_getschedpolicy
pthread_attr_setschedpolicy
功能:
获得/设置线程的调度策略
头文件:
#include <pthread.h>
函数原形:
int pthread_attr_getschedpolicy(const pthread_attr_t *attr,int *policy);
int pthread_attr_setschedpolicy(pthread_attr_t *attr,int policy);
参数:
attr
policy
返回值:
若成功返回0,若失败返回-1。
策略的线程执行了超过一个固定的时期(时间片间隔)没有阻塞,而另外的SCHED_RR或SCHBD_FIPO策略的相同优先级的线程准备好时,运行的线程将被抢占以便准备好的线程可以执行。
五、线程的调度参数
5.
名称::
pthread_attr_getschedparam
pthread_attr_setschedparam
功能:
获得/设置线程的调度参数
头文件:
#include <pthread.h>
函数原形:
int pthread_attr_getschedparam(const pthread_attr_t *attr,struct sched_param *param);
int pthread_attr_setschedparam(pthread_attr_t *attr,const struct sched_param *param);
参数:
attr
param
返回值:
若成功返回0,若失败返回-1。
这两个函数具有两个参数,第1个参数是指向属性对象的指针,第2个参数是sched_param结构或指向该结构的指针。结构sched_param在文件/usr/include /bits/sched.h中定义如下:
struct sched_param
{
};
结构sched_param的子成员sched_priority控制一个优先权值,大的优先权值对应高的优先权。系统支持的最大和最小优先权值可以用sched_get_priority_max函数和sched_get_priority_min函数分别得到。
注意:如果不是编写实时程序,不建议修改线程的优先级。因为,调度策略是一件非常复杂的事情,如果不正确使用会导致程序错误,从而导致死锁等问题。如:在多线程应用程序中为线程设置不同的优先级别,有可能因为共享资源而导致优先级倒置。
6.
名称::
sched_get_priority_max
sched_get_priority_min
功能:
获得系统支持的线程优先权的最大和最小值
头文件:
#include <pthread.h>
函数原形:
int sched_get_priority_max(int policy);
int sched_get_priority_min(int policy);
参数:
policy
返回值:
若成功返回0,若失败返回-1。
#include <pthread.h>
#include <sched.h>
void *child_thread(void *arg)
{
int policy;
int max_priority,min_priority;
struct sched_param param;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
pthread_attr_getinheritsched(&attr,&policy);
if(policy==PTHREAD_EXPLICIT_SCHED)
if(policy==PTHREAD_INHERIT_SCHED)
pthread_attr_setschedpolicy(&attr,SCHED_RR);
pthread_attr_getschedpolicy(&attr,&policy);
if(policy==SCHED_FIFO)
if(policy==SCHED_RR)
if(policy==SCHED_OTHER)
sched_get_priority_max(max_priority);
sched_get_priority_min(min_priority);
printf(“Max priority:%u\n”,max_priority);
printf(“Min priority:%u\n”,min_priority);
param.sched_priority=max_priority;
pthread_attr_setschedparam(&attr,¶m);
printf(“sched_priority:%u\n”,param.sched_priority);
pthread_attr_destroy(&attr);
}
int main(int argc,char *argv[ ])
{
pthread_t child_thread_id;
pthread_create(&child_thread_id,NULL,child_thread,NULL);
pthread_join(child_thread_id,NULL);
}
- 线程属性介绍
- pthread线程属性介绍
- 线程属性介绍(二)
- 线程属性
- 线程属性
- 线程属性
- 线程属性
- 线程属性
- 线程属性
- 线程属性
- 线程属性
- 线程属性
- 线程属性
- 线程属性
- 线程属性
- 线程属性
- 线程与线程属性
- linux 线程 线程属性
- 数组指针和指针数组
- Javascript 动态创建表格(还能删除行列)
- 客户端与服务器通过gson解析json数据
- Phonegap-2.2.0的word Plugin插件编写
- 设置sqlplus中的Edit编辑模式
- 线程属性介绍
- 导入imp
- c#数据库操作类
- Error: unrecognized/unsupported machine ID
- 微软面试题 经典测试(第一组)
- 简单排序算法实现——插入排序
- ThinkPHP import使用方法
- http错误代码含义大全详解
- IOS消息推送