新聞中心

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

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

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


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

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

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


/* thread.c */

#include stdio.h>

#include stdlib.h>

#include pthread.h>


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

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

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


void *thrd_func(void *arg)

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

int thrd_num = (int)arg;

int delay_time = 0;

int count = 0;


printf(Thread %d is startingn, 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 = %dn,

thrd_num, count, delay_time);

}

printf(Thread %d finishedn, 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 = (thread[no], NULL, thrd_func, (void*)no);

if (res != 0)

{

printf(Create thread %d failedn, no);

exit(res);

}

}


printf(Create treads successn 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 joinedn, no);

}

else

{

printf(Thread %d join failedn, no);

}

}

return 0;

}


以下是程序運(yùn)行結(jié)果??梢钥闯雒總€線程的運(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


9.2.2 線程之間的同步與互斥

由于線程共享進(jìn)程的資源和地址空間,因此在對這些資源進(jìn)行操作時,必須考慮到線程間資源訪問的同步與互斥問題。這里主要介紹POSIX中兩種線程同步機(jī)制,分別為互斥鎖和信號量。這兩個同步機(jī)制可以互相通過調(diào)用對方來實現(xiàn),但互斥鎖更適合用于同時可用的資源是惟一的情況;信號量更適合用于同時可用的資源為多個的情況。


1.互斥鎖線程控制

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

互斥鎖是用一種簡單的加鎖方法來控制對共享資源的原子操作。這個互斥鎖只有兩種狀態(tài),也就是上鎖和解鎖,可以把互斥鎖看作某種意義上的全局變量。在同一時刻只能有一個線程掌握某個互斥鎖,擁有上鎖狀態(tài)的線程能夠?qū)蚕碣Y源進(jìn)行操作。若其他線程希望上鎖一個已經(jīng)被上鎖的互斥鎖,則該線程就會掛起,直到上鎖的線程釋放掉互斥鎖為止??梢哉f,這把互斥鎖保證讓每個線程對共享資源按順序進(jìn)行原子操作。


互斥鎖機(jī)制主要包括下面的基本函數(shù)。

n 互斥鎖初始化:pthread_mutex_init()

n 互斥鎖上鎖:pthread_mutex_lock()

n 互斥鎖判斷上鎖:pthread_mutex_trylock()

n 互斥鎖接鎖:pthread_mutex_unlock()

n 消除互斥鎖:pthread_mutex_destroy()


其中,互斥鎖可以分為快速互斥鎖、遞歸互斥鎖和檢錯互斥鎖。這3種鎖的區(qū)別主要在于其他未占有互斥鎖的線程在希望得到互斥鎖時是否需要阻塞等待??焖冁i是指調(diào)用線程會阻塞直至擁有互斥鎖的線程解鎖為止。遞歸互斥鎖能夠成功地返回,并且增加調(diào)用線程在互斥上加鎖的次數(shù),而檢錯互斥鎖則為快速互斥鎖的非阻塞版本,它會立即返回并返回一個錯誤信息。默認(rèn)屬性為快速互斥鎖。


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

表9.5列出了pthread_mutex_init()函數(shù)的語法要點(diǎn)。

表9.5 pthread_mutex_init()函數(shù)語法要點(diǎn)

所需頭文件

#include pthread.h>

函數(shù)原型

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr)

函數(shù)傳入值

mutex:互斥鎖


Mutexattr

PTHREAD_MUTEX_INITIALIZER:創(chuàng)建快速互斥鎖

PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP:創(chuàng)建遞歸互斥鎖

PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP:創(chuàng)建檢錯互斥鎖

函數(shù)返回值

成功:0

出錯:返回錯誤碼


表9.6列出了pthread_mutex_lock()等函數(shù)的語法要點(diǎn)。

表9.6 pthread_mutex_lock()等函數(shù)語法要點(diǎn)

所需頭文件

#include pthread.h>

函數(shù)原型

int pthread_mutex_lock(pthread_mutex_t *mutex,)
int pthread_mutex_trylock(pthread_mutex_t *mutex,)
int pthread_mutex_unlock(pthread_mutex_t *mutex,)
int pthread_mutex_destroy(pthread_mutex_t *mutex,)

函數(shù)傳入值

mutex:互斥鎖

函數(shù)返回值

成功:0

出錯:-1

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

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




評論


相關(guān)推薦

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

關(guān)閉