新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 多線程編程之:Linux線程編程

多線程編程之:Linux線程編程

作者: 時(shí)間:2013-09-13 來(lái)源:網(wǎng)絡(luò) 收藏


本文引用地址:http://butianyuan.cn/article/257101.htm

該程序運(yùn)行結(jié)果如下所示:


$ ./thread_sem

Create treads success

Waiting for threads to finish...

Thread 2 is starting

Thread 2: job 0 delay = 9

Thread 2: job 1 delay = 5

Thread 2: job 2 delay = 10

Thread 2 finished

Thread 2 joined

Thread 1 is starting

Thread 1: job 0 delay = 7

Thread 1: job 1 delay = 4

Thread 1: job 2 delay = 4

Thread 1 finished

Thread 1 joined

Thread 0 is starting

Thread 0: job 0 delay = 10

Thread 0: job 1 delay = 8

Thread 0: job 2 delay = 9

Thread 0 finished

Thread 0 joined


9.2.3 線程屬性

(1)函數(shù)說(shuō)明。

()函數(shù)的第二個(gè)參數(shù)(pthread_attr_t *attr)表示線程的屬性。在上一個(gè)實(shí)例中,將該值設(shè)為NULL,也就是采用默認(rèn)屬性,線程的多項(xiàng)屬性都是可以更改的。這些屬性主要包括綁定屬性、分離屬性、堆棧地址、堆棧大小以及優(yōu)先級(jí)。其中系統(tǒng)默認(rèn)的屬性為非綁定、非分離、缺省1M的堆棧以及與父進(jìn)程同樣級(jí)別的優(yōu)先級(jí)。下面首先對(duì)綁定屬性和分離屬性的基本概念進(jìn)行講解。


n 綁定屬性。

前面已經(jīng)提到,中采用“一對(duì)一”的線程機(jī)制,也就是一個(gè)用戶線程對(duì)應(yīng)一個(gè)內(nèi)核線程。綁定屬性就是指一個(gè)用戶線程固定地分配給一個(gè)內(nèi)核線程,因?yàn)镃PU時(shí)間片的調(diào)度是面向內(nèi)核線程(也就是輕量級(jí)進(jìn)程)的,因此具有綁定屬性的線程可以保證在需要的時(shí)候總有一個(gè)內(nèi)核線程與之對(duì)應(yīng)。而與之對(duì)應(yīng)的非綁定屬性就是指用戶線程和內(nèi)核線程的關(guān)系不是始終固定的,而是由系統(tǒng)來(lái)控制分配的。

n 分離屬性。

分離屬性是用來(lái)決定一個(gè)線程以什么樣的方式來(lái)終止自己。在非分離情況下,當(dāng)一個(gè)線程結(jié)束時(shí),它所占用的系統(tǒng)資源并沒(méi)有被釋放,也就是沒(méi)有真正的終止。只有當(dāng)pthread_join()函數(shù)返回時(shí),創(chuàng)建的線程才能釋放自己占用的系統(tǒng)資源。而在分離屬性情況下,一個(gè)線程結(jié)束時(shí)立即釋放它所占有的系統(tǒng)資源。這里要注意的一點(diǎn)是,如果設(shè)置一個(gè)線程的分離屬性,而這個(gè)線程運(yùn)行又非常快,那么它很可能在()函數(shù)返回之前就終止了,它終止以后就可能將線程號(hào)和系統(tǒng)資源移交給其他的線程使用,這時(shí)調(diào)用()的線程就得到了錯(cuò)誤的線程號(hào)。


這些屬性的設(shè)置都是通過(guò)特定的函數(shù)來(lái)完成的,通常首先調(diào)用pthread_attr_init()函數(shù)進(jìn)行初始化,之后再調(diào)用相應(yīng)的屬性設(shè)置函數(shù),最后調(diào)用pthread_attr_destroy()函數(shù)對(duì)分配的屬性結(jié)構(gòu)指針進(jìn)行清理和回收。設(shè)置綁定屬性的函數(shù)為pthread_attr_setscope(),設(shè)置線程分離屬性的函數(shù)為pthread_attr_setdetachstate(),設(shè)置線程優(yōu)先級(jí)的相關(guān)函數(shù)為pthread_attr_getschedparam()(獲取線程優(yōu)先級(jí))和pthread_attr_setschedparam()(設(shè)置線程優(yōu)先級(jí))。在設(shè)置完這些屬性后,就可以調(diào)用pthread_create()函數(shù)來(lái)創(chuàng)建線程了。


(2)函數(shù)格式。

表9.9列出了pthread_attr_init()函數(shù)的語(yǔ)法要點(diǎn)。

表9.9 pthread_attr_init()函數(shù)語(yǔ)法要點(diǎn)

所需頭文件

#include pthread.h>

函數(shù)原型

int pthread_attr_init(pthread_attr_t *attr)

函數(shù)傳入值

attr:線程屬性結(jié)構(gòu)指針

函數(shù)返回值

成功:0

出錯(cuò):返回錯(cuò)誤碼


表9.10列出了pthread_attr_setscope()函數(shù)的語(yǔ)法要點(diǎn)。

表9.10 pthread_attr_setscope()函數(shù)語(yǔ)法要點(diǎn)

所需頭文件

#include pthread.h>

函數(shù)原型

int pthread_attr_setscope(pthread_attr_t *attr, int scope)

函數(shù)傳入值

attr:線程屬性結(jié)構(gòu)指針

scope

PTHREAD_SCOPE_SYSTEM:綁定

PTHREAD_SCOPE_PROCESS:非綁定

函數(shù)返回值

成功:0

出錯(cuò):-1


表9.11列出了pthread_attr_setdetachstate()函數(shù)的語(yǔ)法要點(diǎn)。

表9.11 pthread_attr_setdetachstate()函數(shù)語(yǔ)法要點(diǎn)

所需頭文件

#include pthread.h>

函數(shù)原型

int pthread_attr_setscope(pthread_attr_t *attr, int detachstate)

函數(shù)傳入值

attr:線程屬性

detachstate

PTHREAD_CREATE_DETACHED:分離

PTHREAD _CREATE_JOINABLE:非分離

函數(shù)返回值

成功:0

出錯(cuò):返回錯(cuò)誤碼


表9.12列出了pthread_attr_getschedparam()函數(shù)的語(yǔ)法要點(diǎn)。

表9.12 pthread_attr_getschedparam()函數(shù)語(yǔ)法要點(diǎn)

所需頭文件

#include pthread.h>

函數(shù)原型

int pthread_attr_getschedparam (pthread_attr_t *attr, struct sched_param *param)

函數(shù)傳入值

attr:線程屬性結(jié)構(gòu)指針

param:線程優(yōu)先級(jí)

函數(shù)返回值

成功:0

出錯(cuò):返回錯(cuò)誤碼


表9.13列出了pthread_attr_setschedparam()函數(shù)的語(yǔ)法要點(diǎn)。

表9.13 pthread_attr_setschedparam()函數(shù)語(yǔ)法要點(diǎn)

所需頭文件

#include pthread.h>

函數(shù)原型

int pthread_attr_setschedparam (pthread_attr_t *attr, struct sched_param *param)

函數(shù)傳入值

attr:線程屬性結(jié)構(gòu)指針

param:線程優(yōu)先級(jí)

函數(shù)返回值

成功:0

出錯(cuò):返回錯(cuò)誤碼

linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)

linux相關(guān)文章:linux教程




評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉