线程属性
属性结构
union pthread_attr_t{
char __size[__SIZEOF_PTHREAD_ATTR_T];
long int __align;
};
属性值不能直接设置,必须使用相关函数进行操作,线程属性主要包括:是否绑定,是否分离,堆栈地址,堆栈大小,优先级
初始化函数
函数原型
/* Initialize thread attribute *ATTR with default attributes
(detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,
no user-provided stack). */
extern int pthread_attr_init (pthread_attr_t *__attr) __THROW __nonnull ((1));
注
:该函数必须在pthread_create()
之前调用
默认:非绑定,非分离,默认1M的堆栈,与父进程同样级别的优先级
关于绑定
轻进程(内核进程)
位于系统层与用户层之间,系统对线程的分配、控制都是通过轻进程来实现的,一个轻进程可以控制一个或者多个线程
非绑定
:默认情况下启动的轻进程以及它所控制的线程都是由系统决定的,所以这就是属于非绑定的
绑定
:把某个线程指定到某个轻进程上面(被绑定的线程具有较高的响应速度)
分离属性
非分离
:原有的线程等待创建的线程结束,只有当pthread_join()
收到返回值时,创建的线程才算终止,才能释放空间
分离
:线程函数执行完以后马上释放空间
绑定函数
/* Return in *SCOPE the scheduling contention scope of *ATTR. */
extern int pthread_attr_getscope (const pthread_attr_t *__restrict __attr,
int *__restrict __scope)
__THROW __nonnull ((1, 2));
第一个参数是指向线程属性结构的指针,
第二个参数是绑定的类型,有两个值:PTHREAD_SCOPE_SYSTEM
(绑定的),PTHREAD_SCOPE_PROCES
(非绑定的)
分离函数
/* Set detach state attribute. */
extern int pthread_attr_setdetachstate (pthread_attr_t *__attr,
int __detachstate)
__THROW __nonnull ((1));
第一个参数是指向线程属性结构的指针,
第二个参数:PTHREAD_CREATE_DETACHED
(分离线程),PTHREAD_CREATE_JOINABLE
(非分离线程)
注
:一般用pthread_cond_timewait()
函数让线程等待一会,留出足够的时间让pthread_create()
返回(被创建的线程中调用)
/* Wait for condition variable COND to be signaled or broadcast until
ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an
absolute time specification; zero is the beginning of the epoch
(00:00:00 GMT, January 1, 1970).
This function is a cancellation point and therefore not marked with
__THROW. */
extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,
pthread_mutex_t *__restrict __mutex,
const struct timespec *__restrict __abstime)
__nonnull ((1, 2, 3));
优先级
/* Data structure to describe a process' schedulability. */
struct sched_param{
int sched_priority;
};
/* Return in *PARAM the scheduling parameters of *ATTR. */
extern int pthread_attr_getschedparam (const pthread_attr_t *__restrict __attr,
struct sched_param *__restrict __param)
__THROW __nonnull ((1, 2));
/* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */
extern int pthread_attr_setschedparam (pthread_attr_t *__restrict __attr,
const struct sched_param *__restrict
__param) __THROW __nonnull ((1, 2));
结构体里面使用一个int存放线程优先级,通过上面两个函数设置和获取线程优先级
Demo
#include <pthread.h>
#include <sched.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
static void *test_thread_fun(void *arg) {
while (1) {
//在这里添加需要执行的代码
fprintf(stderr, "%s(): thread is running!\n", __FUNCTION__);
sleep(1);
}
fprintf(stderr, "%s(): thread terminated!\n", __FUNCTION__);
}
int main(int argc, char **argv) {
pthread_attr_t attr;
pthread_t pid;
struct sched_param param;
int i = 20;
pthread_attr_init(&attr);
pthread_attr_getschedparam(&attr, ¶m);
param.sched_priority = i;
pthread_attr_setschedparam(&attr, ¶m);
//创建一个非绑定,非分离,优先级为20的线程
pthread_create(&pid, &attr, test_thread_fun, NULL);
sleep(20);
pthread_kill(pid);
pthread_join(pid,NULL);
return 0;
}