多線(xiàn)程編程之:Linux線(xiàn)程編程
9.2.2 線(xiàn)程之間的同步與互斥
本文引用地址:http://cafeforensic.com/article/264053.htm由于線(xiàn)程共享進(jìn)程的資源和地址空間,因此在對(duì)這些資源進(jìn)行操作時(shí),必須考慮到線(xiàn)程間資源訪(fǎng)問(wèn)的同步與互斥問(wèn)題。這里主要介紹POSIX中兩種線(xiàn)程同步機(jī)制,分別為互斥鎖和信號(hào)量。這兩個(gè)同步機(jī)制可以互相通過(guò)調(diào)用對(duì)方來(lái)實(shí)現(xiàn),但互斥鎖更適合用于同時(shí)可用的資源是惟一的情況;信號(hào)量更適合用于同時(shí)可用的資源為多個(gè)的情況。
1.互斥鎖線(xiàn)程控制
(1)函數(shù)說(shuō)明。
互斥鎖是用一種簡(jiǎn)單的加鎖方法來(lái)控制對(duì)共享資源的原子操作。這個(gè)互斥鎖只有兩種狀態(tài),也就是上鎖和解鎖,可以把互斥鎖看作某種意義上的全局變量。在同一時(shí)刻只能有一個(gè)線(xiàn)程掌握某個(gè)互斥鎖,擁有上鎖狀態(tài)的線(xiàn)程能夠?qū)蚕碣Y源進(jìn)行操作。若其他線(xiàn)程希望上鎖一個(gè)已經(jīng)被上鎖的互斥鎖,則該線(xiàn)程就會(huì)掛起,直到上鎖的線(xiàn)程釋放掉互斥鎖為止??梢哉f(shuō),這把互斥鎖保證讓每個(gè)線(xiàn)程對(duì)共享資源按順序進(jìn)行原子操作。
互斥鎖機(jī)制主要包括下面的基本函數(shù)。
n 互斥鎖初始化:pthread_mutex_init()
n 互斥鎖上鎖:pthread_mutex_lock()
n 互斥鎖判斷上鎖:pthread_mutex_trylock()
n 互斥鎖接鎖:pthread_mutex_unlock()
n 消除互斥鎖:pthread_mutex_destroy()
其中,互斥鎖可以分為快速互斥鎖、遞歸互斥鎖和檢錯(cuò)互斥鎖。這3種鎖的區(qū)別主要在于其他未占有互斥鎖的線(xiàn)程在希望得到互斥鎖時(shí)是否需要阻塞等待??焖冁i是指調(diào)用線(xiàn)程會(huì)阻塞直至擁有互斥鎖的線(xiàn)程解鎖為止。遞歸互斥鎖能夠成功地返回,并且增加調(diào)用線(xiàn)程在互斥上加鎖的次數(shù),而檢錯(cuò)互斥鎖則為快速互斥鎖的非阻塞版本,它會(huì)立即返回并返回一個(gè)錯(cuò)誤信息。默認(rèn)屬性為快速互斥鎖。
(2)函數(shù)格式。
表9.5列出了pthread_mutex_init()函數(shù)的語(yǔ)法要點(diǎn)。
表9.6列出了pthread_mutex_lock()等函數(shù)的語(yǔ)法要點(diǎn)。
(3)使用實(shí)例。
下面的實(shí)例是在9.2.1小節(jié)示例代碼的基礎(chǔ)上增加互斥鎖功能,實(shí)現(xiàn)原本獨(dú)立與無(wú)序的多個(gè)線(xiàn)程能夠按順序執(zhí)行。
/*thread_mutex.c*/
#include
#include
#include
#define THREAD_NUMBER 3 /* 線(xiàn)程數(shù) */
#define REPEAT_NUMBER 3 /* 每個(gè)線(xiàn)程的小任務(wù)數(shù) */
#define DELAY_TIME_LEVELS 10.0 /*小任務(wù)之間的最大時(shí)間間隔*/
pthread_mutex_t mutex;
void *thrd_func(void *arg)
{
int thrd_num = (int)arg;
int delay_time = 0, count = 0;
int res;
/* 互斥鎖上鎖 */
res = pthread_mutex_lock(&mutex);
if (res)
{
printf("Thread %d lock failedn", thrd_num);
pthread_exit(NULL);
}
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));
/* 互斥鎖初始化 */
pthread_mutex_init(&mutex, NULL);
for (no = 0; no < THREAD_NUMBER; no++)
{
res = pthread_create(&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++)
{
res = pthread_join(thread[no], &thrd_ret);
if (!res)
{
printf("Thread %d joinedn", no);
}
else
{
printf("Thread %d join failedn", no);
}
/* 互斥鎖解鎖 */
pthread_mutex_unlock(&mutex);
}
pthread_mutex_destroy(&mutex);
return 0;
}
該實(shí)例的運(yùn)行結(jié)果如下所示。這里3個(gè)線(xiàn)程之間的運(yùn)行順序跟創(chuàng)建線(xiàn)程的順序相同。
$ ./thread_mutex
Create treads success
Waiting for threads to finish...
Thread 0 is starting
Thread 0: job 0 delay = 7
Thread 0: job 1 delay = 7
Thread 0: job 2 delay = 6
Thread 0 finished
Thread 0 joined
Thread 1 is starting
Thread 1: job 0 delay = 3
Thread 1: job 1 delay = 5
Thread 1: job 2 delay = 10
Thread 1 finished
Thread 1 joined
Thread 2 is starting
Thread 2: job 0 delay = 6
Thread 2: job 1 delay = 10
Thread 2: job 2 delay = 8
Thread 2 finished
Thread 2 joined
linux操作系統(tǒng)文章專(zhuān)題:linux操作系統(tǒng)詳解(linux不再難懂)linux相關(guān)文章:linux教程
塵埃粒子計(jì)數(shù)器相關(guān)文章:塵埃粒子計(jì)數(shù)器原理
評(píng)論