多線程編程之:Linux線程編程
9.2.3 線程屬性
本文引用地址:http://m.ptau.cn/article/264053.htm(1)函數(shù)說明。
pthread_create()函數(shù)的第二個參數(shù)(pthread_attr_t *attr)表示線程的屬性。在上一個實例中,將該值設為NULL,也就是采用默認屬性,線程的多項屬性都是可以更改的。這些屬性主要包括綁定屬性、分離屬性、堆棧地址、堆棧大小以及優(yōu)先級。其中系統(tǒng)默認的屬性為非綁定、非分離、缺省1M的堆棧以及與父進程同樣級別的優(yōu)先級。下面首先對綁定屬性和分離屬性的基本概念進行講解。
n 綁定屬性。
前面已經(jīng)提到,Linux中采用“一對一”的線程機制,也就是一個用戶線程對應一個內(nèi)核線程。綁定屬性就是指一個用戶線程固定地分配給一個內(nèi)核線程,因為CPU時間片的調(diào)度是面向內(nèi)核線程(也就是輕量級進程)的,因此具有綁定屬性的線程可以保證在需要的時候總有一個內(nèi)核線程與之對應。而與之對應的非綁定屬性就是指用戶線程和內(nèi)核線程的關系不是始終固定的,而是由系統(tǒng)來控制分配的。
n 分離屬性。
分離屬性是用來決定一個線程以什么樣的方式來終止自己。在非分離情況下,當一個線程結束時,它所占用的系統(tǒng)資源并沒有被釋放,也就是沒有真正的終止。只有當pthread_join()函數(shù)返回時,創(chuàng)建的線程才能釋放自己占用的系統(tǒng)資源。而在分離屬性情況下,一個線程結束時立即釋放它所占有的系統(tǒng)資源。這里要注意的一點是,如果設置一個線程的分離屬性,而這個線程運行又非常快,那么它很可能在pthread_create()函數(shù)返回之前就終止了,它終止以后就可能將線程號和系統(tǒng)資源移交給其他的線程使用,這時調(diào)用pthread_create()的線程就得到了錯誤的線程號。
這些屬性的設置都是通過特定的函數(shù)來完成的,通常首先調(diào)用pthread_attr_init()函數(shù)進行初始化,之后再調(diào)用相應的屬性設置函數(shù),最后調(diào)用pthread_attr_destroy()函數(shù)對分配的屬性結構指針進行清理和回收。設置綁定屬性的函數(shù)為pthread_attr_setscope(),設置線程分離屬性的函數(shù)為pthread_attr_setdetachstate(),設置線程優(yōu)先級的相關函數(shù)為pthread_attr_getschedparam()(獲取線程優(yōu)先級)和pthread_attr_setschedparam()(設置線程優(yōu)先級)。在設置完這些屬性后,就可以調(diào)用pthread_create()函數(shù)來創(chuàng)建線程了。
(2)函數(shù)格式。
表9.9列出了pthread_attr_init()函數(shù)的語法要點。
表9.10列出了pthread_attr_setscope()函數(shù)的語法要點。
表9.11列出了pthread_attr_setdetachstate()函數(shù)的語法要點。
表9.12 pthread_attr_getschedparam()函數(shù)語法要點
表9.13列出了pthread_attr_setschedparam()函數(shù)的語法要點。
(3)使用實例。
下面的實例是在我們已經(jīng)很熟悉的實例的基礎上增加線程屬性設置的功能。為了避免不必要的復雜性,這里就創(chuàng)建一個線程,這個線程具有綁定和分離屬性,而且主線程通過一個finish_flag標志變量來獲得線程結束的消息,而并不調(diào)用pthread_join()函數(shù)。
/*thread_attr.c*/
#include
#include
#include
#define REPEAT_NUMBER 3 /* 線程中的小任務數(shù) */
#define DELAY_TIME_LEVELS 10.0 /* 小任務之間的最大時間間隔 */
int finish_flag = 0;
void *thrd_func(void *arg)
{
int delay_time = 0;
int count = 0;
printf("Thread is startingn");
for (count = 0; count < REPEAT_NUMBER; count++)
{
delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)) + 1;
sleep(delay_time);
printf("tThread : job %d delay = %dn", count, delay_time);
}
printf("Thread finishedn");
finish_flag = 1;
pthread_exit(NULL);
}
int main(void)
{
pthread_t thread;
pthread_attr_t attr;
int no = 0, res;
void * thrd_ret;
srand(time(NULL));
/* 初始化線程屬性對象 */
res = pthread_attr_init(&attr);
if (res != 0)
{
printf("Create attribute failedn");
exit(res);
}
/* 設置線程綁定屬性 */
res = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
/* 設置線程分離屬性 */
res += pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (res != 0)
{
printf("Setting attribute failedn");
exit(res);
}
res = pthread_create(&thread, &attr, thrd_func, NULL);
if (res != 0)
{
printf("Create thread failedn");
exit(res);
}
/* 釋放線程屬性對象 */
pthread_attr_destroy(&attr);
printf("Create tread successn");
while(!finish_flag)
{
printf("Waiting for thread to finish...n");
sleep(2);
}
return 0;
}
接下來可以在線程運行前后使用“free”命令查看內(nèi)存的使用情況。以下是運行結果:
$ ./thread_attr
Create tread success
Waiting for thread to finish...
Thread is starting
Waiting for thread to finish...
Thread : job 0 delay = 3
Waiting for thread to finish...
Thread : job 1 delay = 2
Waiting for thread to finish...
Waiting for thread to finish...
Waiting for thread to finish...
Waiting for thread to finish...
Thread : job 2 delay = 9
Thread finished
/* 程序運行之前 */
$ free
total used free shared buffers cached
Mem: 255556 191940 63616 10 5864 61360
-/+ buffers/cache: 124716 130840
Swap: 377488 18352 359136
/* 程序運行之中 */
$ free
total used free shared buffers cached
Mem: 255556 191948 63608 10 5888 61336
-/+ buffers/cache: 124724 130832
Swap: 377488 18352 359136
/* 程序運行之后 */
$ free
total used free shared buffers cached
Mem: 255556 191940 63616 10 5904 61320
-/+ buffers/cache: 124716 130840
Swap: 377488 18352 359136
可以看到,線程在運行結束后就收回了系統(tǒng)資源,并釋放內(nèi)存。
linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)linux相關文章:linux教程
塵埃粒子計數(shù)器相關文章:塵埃粒子計數(shù)器原理
評論