首頁(yè) 新聞 > 科技 > 正文

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

9.2 Linux線程編程

9.2.1 線程基本編程

這里要講的線程相關(guān)操作都是用戶空間中的線程的操作。在Linux中,一般pthread線程庫(kù)是一套通用的線程庫(kù),是由POSIX提出的,因此具有很好的可移植性。

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

創(chuàng)建線程實(shí)際上就是確定調(diào)用該線程函數(shù)的入口點(diǎn),這里通常使用的函數(shù)是pthread_create()。在線程創(chuàng)建以后,就開始運(yùn)行相關(guān)的線程函數(shù),在該函數(shù)運(yùn)行完之后,該線程也就退出了,這也是線程退出一種方法。另一種退出線程的方法是使用函數(shù)pthread_exit(),這是線程的主動(dòng)行為。這里要注意的是,在使用線程函數(shù)時(shí),不能隨意使用exit()退出函數(shù)進(jìn)行出錯(cuò)處理,由于exit()的作用是使調(diào)用進(jìn)程終止,往往一個(gè)進(jìn)程包含多個(gè)線程,因此,在使用exit()之后,該進(jìn)程中的所有線程都終止了。因此,在線程中就可以使用pthread_exit()來(lái)代替進(jìn)程中的exit()。

由于一個(gè)進(jìn)程中的多個(gè)線程是共享數(shù)據(jù)段的,因此通常在線程退出之后,退出線程所占用的資源并不會(huì)隨著線程的終止而得到釋放。正如進(jìn)程之間可以用wait()系統(tǒng)調(diào)用來(lái)同步終止并釋放資源一樣,線程之間也有類似機(jī)制,那就是pthread_join()函數(shù)。pthread_join()可以用于將當(dāng)前線程掛起來(lái)等待線程的結(jié)束。這個(gè)函數(shù)是一個(gè)線程阻塞的函數(shù),調(diào)用它的函數(shù)將一直等待到被等待的線程結(jié)束為止,當(dāng)函數(shù)返回時(shí),被等待線程的資源就被收回。

前面已提到線程調(diào)用pthread_exit()函數(shù)主動(dòng)終止自身線程。但是在很多線程應(yīng)用中,經(jīng)常會(huì)遇到在別的線程中要終止另一個(gè)線程的執(zhí)行的問(wèn)題。此時(shí)調(diào)用pthread_cancel()函數(shù)實(shí)現(xiàn)這種功能,但在被取消的線程的內(nèi)部需要調(diào)用pthread_setcancel()函數(shù)和pthread_setcanceltype()函數(shù)設(shè)置自己的取消狀態(tài),例如被取消的線程接收到另一個(gè)線程的取消請(qǐng)求之后,是接受還是忽略這個(gè)請(qǐng)求;如果接受,是立刻進(jìn)行終止操作還是等待某個(gè)函數(shù)的調(diào)用等。

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

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

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

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

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

(3)函數(shù)使用。

以下實(shí)例中創(chuàng)建了3個(gè)線程,為了更好地描述線程之間的并行執(zhí)行,讓3個(gè)線程重用同一個(gè)執(zhí)行函數(shù)。每個(gè)線程都有5次循環(huán)(可以看成5個(gè)小任務(wù)),每次循環(huán)之間會(huì)隨機(jī)等待1~10s的時(shí)間,意義在于模擬每個(gè)任務(wù)的到達(dá)時(shí)間是隨機(jī)的,并沒(méi)有任何特定規(guī)律。

/* thread.c */

#include

#include

#include

#define THREAD_NUMBER 3 /*線程數(shù)*/

#define REPEAT_NUMBER 5 /*每個(gè)線程中的小任務(wù)數(shù)*/

#define DELAY_TIME_LEVELS 10.0 /*小任務(wù)之間的最大時(shí)間間隔*/

void *thrd_func(void *arg)

{ /* 線程函數(shù)例程 */

int thrd_num = (int)arg;

int delay_time = 0;

int count = 0;

printf("Thread %d is starting\n", thrd_num);

for (count = 0; count < REPEAT_NUMBER; count++)

{

delay_time = (int)(rand() * DELAY_TIME_LEVELS/(RAND_MAX)) + 1;

sleep(delay_time);

printf("\tThread %d: job %d delay = %d\n",

thrd_num, count, delay_time);

}

printf("Thread %d finished\n", thrd_num);

pthread_exit(NULL);

}

int main(void)

{

pthread_t thread[THREAD_NUMBER];

int no = 0, res;

void * thrd_ret;

srand(time(NULL));

for (no = 0; no < THREAD_NUMBER; no++)

{

/* 創(chuàng)建多線程*/

res = pthread_create(&thread[no], NULL, thrd_func, (void*)no);

if (res != 0)

{

printf("Create thread %d failed\n", no);

exit(res);

}

}

printf("Create treads success\n Waiting for threads to finish...\n");

for (no = 0; no < THREAD_NUMBER; no++)

{

/* 等待線程結(jié)束 */

res = pthread_join(thread[no], &thrd_ret);

if (!res)

{

printf("Thread %d joined\n", no);

}

else

{

printf("Thread %d join failed\n", no);

}

}

return 0;

}

以下是程序運(yùn)行結(jié)果??梢钥闯雒總€(gè)線程的運(yùn)行和結(jié)束是獨(dú)立與并行的。

$ ./thread

Create treads success

Waiting for threads to finish...

Thread 0 is starting

Thread 1 is starting

Thread 2 is starting

Thread 1: job 0 delay = 6

Thread 2: job 0 delay = 6

Thread 0: job 0 delay = 9

Thread 1: job 1 delay = 6

Thread 2: job 1 delay = 8

Thread 0: job 1 delay = 8

Thread 2: job 2 delay = 3

Thread 0: job 2 delay = 3

Thread 2: job 3 delay = 3

Thread 2: job 4 delay = 1

Thread 2 finished

Thread 1: job 2 delay = 10

Thread 1: job 3 delay = 4

Thread 1: job 4 delay = 1

Thread 1 finished

Thread 0: job 3 delay = 9

Thread 0: job 4 delay = 2

Thread 0 finished

Thread 0 joined

Thread 1 joined

Thread 2 joined

關(guān)鍵詞: Linux 多線程 互斥

最近更新

關(guān)于本站 管理團(tuán)隊(duì) 版權(quán)申明 網(wǎng)站地圖 聯(lián)系合作 招聘信息

Copyright © 2005-2018 創(chuàng)投網(wǎng) - www.mallikadua.com All rights reserved
聯(lián)系我們:33 92 950@qq.com
豫ICP備2020035879號(hào)-12