互斥鎖的概念和使用方法。

2021-06-21 13:41:49 字數 3499 閱讀 2678

在單執行緒條件下,由於對資料操作,在同樣的時間下,只有乙個執行緒來操作。所以不用擔心資料的同步問題。現代的作業系統,大都提供併發機制,雖然有時候是表面的併發。在linux中,併發用的最多的是基於執行緒的併發,程序的代價太高了,這樣,乙個共享的資料,在同一時間內,可能有多個執行緒在操作。如果沒有同步機制,那麼想要保證每個執行緒操作的正確性,是很困難的。

1互斥鎖概念:

互斥鎖提供乙個可以在同一時間,只讓乙個執行緒訪問臨界資源的的操作介面。互斥鎖(mutex)是個提供執行緒同步的基本鎖。讓上鎖後,其他的執行緒如果想要鎖上,那麼會被

阻塞

,直到鎖釋放後(說明,一般會把訪問共享記憶體這段**放在上鎖程式之後。)。

如果,在鎖釋放後,有多個執行緒被阻塞,那麼,所有的被阻塞的執行緒會被設為可執行狀態。第乙個執行的執行緒,取得鎖的控制權,上鎖。其他的執行緒繼續阻塞。

2:互斥鎖系統原型

互斥鎖的系統原型為:pthread_mutex_t,在用互斥鎖之前,必須要初始化互斥鎖,可以呼叫pthread_mutex_init;或者是pthread_mutex_initialzer(僅用於靜態分配記憶體)如果我們動態分配互斥鎖(比如,用malloc),那麼,在釋放記憶體之前,必須呼叫pthread_mutex_destroy;

下面為互斥鎖初始化和銷毀的函式原型:

#include 

int pthread_mutex_init(pthread_mutex_t *restrict mutex,

const pthread_mutexattr_t *restrict attr);

int pthread_mutex_destroy(pthread_mutex_t *mutex);

both return: 0 if ok, error number on failure

互斥鎖的上鎖和解鎖的函式原型為:

#include 

int pthread_mutex_lock(pthread_mutex_t *mutex);

int pthread_mutex_trylock(pthread_mutex_t *mutex);

int pthread_mutex_unlock(pthread_mutex_t *mutex);

all return: 0 if ok, error number on failure

如果要鎖上乙個互斥鎖用pthread_mutex_lock;解鎖用pthread_mutex_unlock;如果,乙個執行緒不想被阻止,那麼可以用pthread_mutex_trylock函式來上鎖。如果乙個執行緒呼叫pthread_mutex_trylock時,鎖變數沒有被其他的執行緒鎖上,那麼pthread_mutex_trylock會鎖上鎖變數,返回值0,表示成功。否則,pthread_mutex_trylock失敗,返回ebusy,沒有上鎖。

[cpp]view plain

copy

#include

#include

void

fun_thread1(

char

* msg);  

void

fun_thread2(

char

* msg);  

intg_value = 1;  

pthread_mutex_t mutex;  

/*in the thread individual, the thread reset the g_value to 0,and add to 5 int the thread1,add to 6 in the thread2.*/

intmain(

intargc, 

char

* argv)  

if(pthread_create(&thread1,null,(

void

*)fun_thread1,null) != 0)  

if(pthread_create(&thread2,null,(

void

*)fun_thread2,null) != 0)  

sleep(1);  

printf("i am main thread, g_vlaue is %d./n"

,g_value);  

return

0;  

}  void

fun_thread1(

char

* msg)  

g_value = 0;/*reset the g_value to 0.after that add it to 5.*/

printf("thread 1 locked,init the g_value to 0, and add 5./n"

);  

g_value += 5;  

printf("the g_value is %d./n"

,g_value);  

pthread_mutex_unlock(&mutex);/*unlock the mutex*/

printf("thread 1 unlocked./n"

);  

}  void

fun_thread2(

char

* msg)  

g_value = 0;/*reset the g_value to 0.after that add it to 6.*/

printf("thread 2 locked,init the g_value to 0, and add 6./n"

);  

g_value += 6;  

printf("the g_value is %d./n"

,g_value);  

pthread_mutex_unlock(&mutex);/*unlock the mutex*/

printf("thread 2 unlocked./n"

);  

}  

在ubuntu10.4,

thread model: posix

,gcc version 4.4.3 (ubuntu 4.4.3-4ubuntu5)

編譯,執行如下:

thread 2 locked,init the g_value to 0, and add 6.

the g_value is 6.

thread 2 unlocked.

thread 1 locked,init the g_value to 0, and add 5.

the g_value is 5.

thread 1 unlocked.

i am main thread, g_vlaue is 5.

總結:

關於互斥鎖,可能有些地方,不太容易懂。比如,互斥鎖鎖什麼?簡單的來說,互斥鎖用的限制在同一時刻,其他的執行緒執行

pthread_mutex_lock

和pthread_mutex_unlock

之間的指令。

互斥鎖mutex的使用方法

一,鎖的建立 鎖可以被動態或靜態建立,可以用巨集pthread mutex initializer來靜態的初始化鎖,採用這種方式比較容易理解,互斥鎖是pthread mutex t的結構體,而這個巨集是乙個結構常量,如下可以完成靜態的初始化鎖 pthread mutex t mutex pthrea...

linux互斥鎖的使用方法

include include include include include include include include include include include include pthread mutex t mutex int val 0 void threadfunc void a...

c 互斥鎖mutex使用方法

多執行緒修改基本資料型別時,也可能出現同步問題,哪怕時最簡單的累加操作。通過mutex在對同乙個資料操作時加鎖,實現了對資源的獨佔。pragma once include include include include using namespace std class mutextest prin...