这一篇中,主要说说pthread_create函数的第二个参数,即关于线程属性的设置。这些属性主要包括邦定属性、分离属性、堆栈地址、堆栈大小、优先级。其中系统默认的是非邦定、非分离、缺省1M的堆栈、与父进程同样级别的优先级。在
pthread_create中,把第二个参数设置为NULL的话,将采用默认的属性配置。
邦定属性
在LINUX中,采用的是“一对一”的线程机制。也就是一个用户线程对应一个内核线程。邦定属性就是指一个用户线程固定地分配给一个内核线程,因为CPU时间片的调度是面向内核线程(轻量级进程)的,因此具有邦定属性的线程可以保证在需要的时候总有一个内核线程与之对应,而与之对应的非邦定属性就是指用户线程和内核线程的关系不是始终固定的,而是由系统来分配。
分离属性
分离属性是决定以一个什么样的方式来终止自己。在非分离情况下,当一个线程结束时,它多占用的线程没有得到释放,也就是没有真正的终止,需要通过pthread_join来释放资源。而在分离属性情况下,一个线程结束时会立即释放它所占有的系统资源。但这里有一点要注意的是,如果设置一个线程分离属性,而这个线程又运行得非常快的话,那么它很可能在pthread_create函数返回之前就终止了线程函数的运行,它终止以后就很有可能将线程号和系统资源移交给其他的线程使用,这时调用pthread_create的线程就得到错误的线程号。
这些属性都是通过一些函数来完成的,通常先调用pthread_attr_init来初始化,之后来调用相应的属性设置函数。
pthread_attr_init
功能: 对线程属性变量的初始化。
函数原型: int pthread_attr_init (pthread_attr_t* attr);
pthread_attr_setscope
功能: 设置线程绑定属性。
函数原型: int pthread_attr_setscope (pthread_attr_t* attr, int scope);
pthread_attr_setdetachstate
功能: 设置线程分离属性。
函数原型: int pthread_attr_setdetachstate (pthread_attr_t* attr, int detachstate);
pthread_attr_getschedparam
功能: 得到线程优先级。
函数原型: int pthread_attr_getschedparam (pthread_attr_t* attr,
struct sched_param* param);
pthread_attr_setschedparam
功能: 设置线程优先级。
函数原型: int pthread_attr_setschedparam (pthread_attr_t* attr,
struct sched_param* param);
程序实例如下:
#include
#include
#include
#include
void *pthread_function1(void *arg);
void *pthread_function2(void *arg);
int main (int argc, char** argv) {
pthread_t pt_1 = 0;
pthread_t pt_2 = 0;
int ret = 0;
char message[] = "hello!";
void *thread_result;
pthread_attr_t attr = { 0 };
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
ret = pthread_create(&pt_1, &attr, pthread_function1, (void *)message);
if(0 != ret) {
perror("pthread1 creation failed!");
}
ret = pthread_create(&pt_2, NULL, pthread_function2, (void *)message);
if(0 != ret) {
perror("pthread2 creation failed!");
}
//pthread_join(pt_1, NULL);
//pthread_join(pt_2, NULL);
pthread_join(pt_2, &thread_result);
printf("Thread2 returns %s\n", (char*)thread_result);
return 0;
}
void *pthread_function1(void *arg) {
printf("This is thread1, %s\n", (char*)arg);
sleep(3);
pthread_exit("Thank you for using thread1");
}
void *pthread_function2(void *arg) {
printf("This is thread2, %s\n", (char*)arg);
sleep(3);
pthread_exit("Thank you for using thread2");
}