1. 初始化:
在linux下, 線程的互斥量數據類型是pthread_mutex_t. 在使用前, 要對它進行初始化:
對于靜態分配的互斥量, 可以把它設置為PTHREAD_MUTEX_INITIALIZER, 或者調用pthread_mutex_init.
對于動態分配的互斥量, 在申請內存(malloc)之后, 通過pthread_mutex_init進行初始化, 并且在釋放內存(free)前需要調用pthread_mutex_destroy.
原型:
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restric attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
頭文件:
返回值: 成功則返回0, 出錯則返回錯誤編號.
說明: 如果使用默認的屬性初始化互斥量, 只需把attr設為NULL. 其他值在以后講解.
2. 互斥操作:
對共享資源的訪問, 要對互斥量進行加鎖, 如果互斥量已經上了鎖, 調用線程會阻塞, 直到互斥量被解鎖. 在完成了對共享資源的訪問后, 要對互斥量進行解鎖.
首先說一下加鎖函數:
頭文件:
原型:
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
返回值: 成功則返回0, 出錯則返回錯誤編號.
說明: 具體說一下trylock函數, 這個函數是非阻塞調用模式, 也就是說, 如果互斥量沒被鎖住, trylock函數將把互斥量加鎖, 并獲得對共享資源的訪問權限; 如果互斥量被鎖住了, trylock函數將不會阻塞等待而直接返回EBUSY, 表示共享資源處于忙狀態.
再說一下解所函數:
頭文件:
原型: int pthread_mutex_unlock(pthread_mutex_t *mutex);
返回值: 成功則返回0, 出錯則返回錯誤編號.
3. 死鎖:
死鎖主要發生在有多個依賴鎖存在時, 會在一個線程試圖以與另一個線程相反順序鎖住互斥量時發生. 如何避免死鎖是使用互斥量應該格外注意的東西.
總體來講, 有幾個不成文的基本原則:
對共享資源操作前一定要獲得鎖.
完成操作以后一定要釋放鎖.
盡量短時間地占用鎖.
如果有多鎖, 如獲得順序是ABC連環扣, 釋放順序也應該是ABC.
線程錯誤返回時應該釋放它所獲得的鎖.
示例:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <errno.h>pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;int lock_var;time_t end_time;int sum;void pthread1(void *arg);void pthread2(void *arg);void pthread3(void *arg);int main(int argc, char *argv[]){pthread_t id1,id2,id3;pthread_t mon_th_id;int ret;sum=10;end_time = time(NULL)+10;pthread_mutex_init(&mutex,NULL);ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);if(ret!=0)perror("pthread cread1");ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);if(ret!=0)perror("pthread cread2");ret=pthread_create(&id3,NULL,(void *)pthread3, NULL);if(ret!=0)perror("pthread cread3");pthread_join(id1,NULL);pthread_join(id2,NULL);pthread_join(id3,NULL);exit(0);}void pthread1(void *arg){int i;while(time(NULL) < end_time){if(pthread_mutex_lock(&mutex)!=0) //lock{perror("pthread_mutex_lock");}elsePRintf("pthread1:pthread1 lock the variable/n");for(i=0;i<2;i++){sleep(2);lock_var++;}if(pthread_mutex_unlock(&mutex)!=0) //unlock{perror("pthread_mutex_unlock");}elseprintf("pthread1:pthread1 unlock the variable/n");sleep(1);}}void pthread2(void *arg){int nolock=0;int ret;while(time(NULL) < end_time){ret=pthread_mutex_trylock(&mutex);//try lockif(ret==EBUSY)printf("pthread2:the variable is locked by pthread1/n");else{if(ret!=0){perror("pthread_mutex_trylock");exit(1);}elseprintf("pthread2:pthread2 got lock.The variable is %d/n",lock_var);if(pthread_mutex_unlock(&mutex)!=0)//unlock{perror("pthread_mutex_unlock");}elseprintf("pthread2:pthread2 unlock the variable/n");}sleep(1);}}void pthread3(void *arg){/*int nolock=0;int ret;while(time(NULL) < end_time){ret=pthread_mutex_trylock(&mutex);if(ret==EBUSY)printf("pthread3:the variable is locked by pthread1 or 2/n");else{if(ret!=0){perror("pthread_mutex_trylock");exit(1);}elseprintf("pthread3:pthread3 got lock.The variable is %d/n",lock_var);if(pthread_mutex_unlock(&mutex)!=0){perror("pthread_mutex_unlock");}elseprintf("pthread3:pthread2 unlock the variable/n");}sleep(3);}*/} |