Linux互斥鎖之線程互斥鎖
在編程中,引入了對象互斥鎖的概念,來保證共享數(shù)據(jù)操作的完整性。每個對象都對應(yīng)于一個可稱為" 互斥鎖" 的標記,這個標記用來保證在任一時刻,只能有一個線程訪問該對象。
Linux實現(xiàn)的互斥鎖機制包括POSIX互斥鎖和內(nèi)核互斥鎖,本文主要講POSIX互斥鎖,即線程間互斥鎖。
“ 信號量用在多線程多任務(wù)同步的,一個線程完成了某一個動作就通過信號量告訴別的線程,別的線程再進行某些動作(大家都在sem_wait的時候,就阻塞在 那里)。而互斥鎖是用在多線程多任務(wù)互斥的,一個線程占用了某一個資源,那么別的線程就無法訪問,直到這個線程unlock,其他的線程才開始可以利用這 個資源。比如對全局變量的訪問,有時要加鎖,操作完了,在解鎖。有的時候鎖和信號量會同時使用的”
也就是說,信號量不一定是鎖定某一個資源,而是 流程上的概念,比如:有A,B兩個線程,B線程要等A線程完成某一任務(wù)以后再進行自己下面的步驟,這個任務(wù)并不一定是鎖定某一資源,還可以是進行一些計算 或者數(shù)據(jù)處理之類。而線程互斥量則是“鎖住某一資源”的概念,在鎖定期間內(nèi),其他線程無法對被保護的數(shù)據(jù)進行操作。在有些情況下兩者可以互換。
兩者之間的區(qū)別:
作用域
信號量 : 進程間或線程間(linux僅線程間)
互斥鎖 : 線程間
上鎖時
信號量 : 只要信號量的value大于0,其他線程就可以sem_wait成功,成功后信號量的value減一。若value值不大于0,則sem_wait阻塞,直到sem_post釋放后value值加一。一句話,信號量的value>=0 。
互斥鎖 : 只要被鎖住,其他任何線程都不可以訪問被保護的資源。如果沒有鎖,獲得資源成功,否則進行阻塞等待資源可用。一句話,線程互斥鎖的vlaue可以為負數(shù) 。
多線程
線程是計算機中獨立運行的最小單位,運行時占用很少的系統(tǒng)資源。與多進程相比,多進程具有多進程不具備的一些優(yōu)點,其最重要的是:對于多線程來說,其能夠比多進程更加節(jié)省資源。
線程創(chuàng)建
在Linux中,新建的線程并不是在原先的進程中,而是系統(tǒng)通過一個系統(tǒng)調(diào)用clone()。該系統(tǒng)copy了一個和原先進程完全一樣的進程,并在這個進程中執(zhí)行線程函數(shù)。
在Linux中,通過函數(shù)pthread_create()函數(shù)實現(xiàn)線程的創(chuàng)建:
pthread_create()
- int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*st
其中:
- thread表示的是一個pthread_t類型的指針;
- attr用于指定線程的一些屬性;
- start_routine表示的是一個函數(shù)指針,該函數(shù)是線程調(diào)用函數(shù);
- arg表示的是傳遞給線程調(diào)用函數(shù)的參數(shù)。
當線程創(chuàng)建成功時,函數(shù)pthread_create()返回0,若返回值不為0則表示創(chuàng)建線程失敗。對于線程的屬性,則在結(jié)構(gòu)體pthread_attr_t中定義。
線程創(chuàng)建的過程如下所示:
- #include <stdio.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <malloc.h>
- void* thread(void *id){
- pthread_t newthid;
- newthid = pthread_self();
- printf("this is a new thread, thread ID is %u\n", newthid);
- return NULL;
- }
- int main(){
- int num_thread = 5;
- pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread);
- printf("main thread, ID is %u\n", pthread_self());
- for (int i = 0; i < num_thread; i++){
- if (pthread_create(&pt[i], NULL, thread, NULL) != 0){
- printf("thread create failed!\n");
- return 1;
- }
- }
- sleep(2);
- free(pt);
- return 0;
- }
在上述代碼中,使用到了pthread_self()函數(shù),該函數(shù)的作用是獲取本線程的線程ID。在主函數(shù)中的sleep()用于將主進程處于等待狀態(tài),以讓線程執(zhí)行完成。最終的執(zhí)行效果如下所示:
那么,如何利用arg向子線程傳遞參數(shù)呢?其具體的實現(xiàn)如下所示:
- #include <stdio.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <malloc.h>
- void* thread(void *id){
- pthread_t newthid;
- newthid = pthread_self();
- int num = *(int *)id;
- printf("this is a new thread, thread ID is %u,id:%d\n", newthid, num);
- return NULL;
- }
- int main(){
- //pthread_t thid;
- int num_thread = 5;
- pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread);
- int * id = (int *)malloc(sizeof(int) * num_thread);
- printf("main thread, ID is %u\n", pthread_self());
- for (int i = 0; i < num_thread; i++){
- id[i] = i;
- if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){
- printf("thread create failed!\n");
- return 1;
- }
- }
- sleep(2);
- free(pt);
- free(id);
- return 0;
- }
其最終的執(zhí)行效果如下圖所示:
如果在主進程提前結(jié)束,會出現(xiàn)什么情況呢?如下述的代碼:
- #include <stdio.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <malloc.h>
- void* thread(void *id){
- pthread_t newthid;
- newthid = pthread_self();
- int num = *(int *)id;
- printf("this is a new thread, thread ID is %u,id:%d\n", newthid, num);
- sleep(2);
- printf("thread %u is done!\n", newthid);
- return NULL;
- }
- int main(){
- //pthread_t thid;
- int num_thread = 5;
- pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread);
- int * id = (int *)malloc(sizeof(int) * num_thread);
- printf("main thread, ID is %u\n", pthread_self());
- for (int i = 0; i < num_thread; i++){
- id[i] = i;
- if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){
- printf("thread create failed!\n");
- return 1;
- }
- }
- //sleep(2);
- free(pt);
- free(id);
- return 0;
- }
此時,主進程提前結(jié)束,進程會將資源回收,此時,線程都將退出執(zhí)行,運行結(jié)果如下所示:
線程掛起
在上述的實現(xiàn)過程中,為了使得主線程能夠等待每一個子線程執(zhí)行完成后再退出,使用了free()函數(shù),在Linux的多線程中,也可以使用pthread_join()函數(shù)用于等待其他線程,函數(shù)的具體形式為:
- int pthread_join(pthread_t thread, void **retval);
函數(shù)pthread_join()用來等待一個線程的結(jié)束,其調(diào)用這將被掛起。
一個線程僅允許一個線程使用pthread_join()等待它的終止。
如需要在主線程中等待每一個子線程的結(jié)束,如下述代碼所示:
- #include <stdio.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <malloc.h>
- void* thread(void *id){
- pthread_t newthid;
- newthid = pthread_self();
- int num = *(int *)id;
- printf("this is a new thread, thread ID is %u,id:%d\n", newthid, num);
- printf("thread %u is done\n", newthid);
- return NULL;
- }
- int main(){
- int num_thread = 5;
- pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread);
- int * id = (int *)malloc(sizeof(int) * num_thread);
- printf("main thread, ID is %u\n", pthread_self());
- for (int i = 0; i < num_thread; i++){
- id[i] = i;
- if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){
- printf("thread create failed!\n");
- return 1;
- }
- }
- for (int i = 0; i < num_thread; i++){
- pthread_join(pt[i], NULL);
- }
- free(pt);
- free(id);
- return 0;
- }
最終的執(zhí)行效果如下所示:

注:在編譯的時候需要鏈接libpthread.a:
g++ xx.c -lpthread -o xx
互斥鎖mutex
多線程的問題引入
多線程的最大的特點是資源的共享,但是,當多個線程同時去操作(同時去改變)一個臨界資源時,會破壞臨界資源。如利用多線程同時寫一個文件:
- #include <stdio.h>
- #include <pthread.h>
- #include <malloc.h>
- const char filename[] = "hello";
- void* thread(void *id){
- int num = *(int *)id;
- // 寫文件的操作
- FILE *fp = fopen(filename, "a+");
- int start = *((int *)id);
- int end = start + 1;
- setbuf(fp, NULL);// 設(shè)置緩沖區(qū)的大小
- fprintf(stdout, "%d\n", start);
- for (int i = (start * 10); i < (end * 10); i ++){
- fprintf(fp, "%d\t", i);
- }
- fprintf(fp, "\n");
- fclose(fp);
- return NULL;
- }
- int main(){
- int num_thread = 5;
- pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread);
- int * id = (int *)malloc(sizeof(int) * num_thread);
- for (int i = 0; i < num_thread; i++){
- id[i] = i;
- if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){
- printf("thread create failed!\n");
- return 1;
- }
- }
- for (int i = 0; i < num_thread; i++){
- pthread_join(pt[i], NULL);
- }
- // 釋放資源
- free(pt);
- free(id);
- return 0;
- }
執(zhí)行以上的代碼,我們會發(fā)現(xiàn),得到的結(jié)果是混亂的,出現(xiàn)上述的最主要的原因是,我們在編寫多線程代碼的過程中,每一個線程都嘗試去寫同一個文件,這樣便出現(xiàn)了上述的問題,這便是共享資源的同步問題,在Linux編程中,線程同步的處理方法包括:信號量,互斥鎖和條件變量。
互斥鎖
互斥鎖是通過鎖的機制來實現(xiàn)線程間的同步問題?;コ怄i的基本流程為:
- 初始化一個互斥鎖:pthread_mutex_init()函數(shù)
- 加鎖:pthread_mutex_lock()函數(shù)或者pthread_mutex_trylock()函數(shù)
- 對共享資源的操作
- 解鎖:pthread_mutex_unlock()函數(shù)
- 注銷互斥鎖:pthread_mutex_destory()函數(shù)
其中,在加鎖過程中,pthread_mutex_lock()函數(shù)和pthread_mutex_trylock()函數(shù)的過程略有不同:
- 當使用pthread_mutex_lock()函數(shù)進行加鎖時,若此時已經(jīng)被鎖,則嘗試加鎖的線程會被阻塞,直到互斥鎖被其他線程釋放,當pthread_mutex_lock()函數(shù)有返回值時,說明加鎖成功;
- 而使用pthread_mutex_trylock()函數(shù)進行加鎖時,若此時已經(jīng)被鎖,則會返回EBUSY的錯誤碼。
同時,解鎖的過程中,也需要滿足兩個條件:
- 解鎖前,互斥鎖必須處于鎖定狀態(tài);
- 必須由加鎖的線程進行解鎖。
當互斥鎖使用完成后,必須進行清除。
有了以上的準備,我們重新實現(xiàn)上述的多線程寫操作,其實現(xiàn)代碼如下所示:
- #include <stdio.h>
- #include <pthread.h>
- #include <malloc.h>
- pthread_mutex_t mutex;
- const char filename[] = "hello";
- void* thread(void *id){
- int num = *(int *)id;
- // 加鎖
- if (pthread_mutex_lock(&mutex) != 0){
- fprintf(stdout, "lock error!\n");
- }
- // 寫文件的操作
- FILE *fp = fopen(filename, "a+");
- int start = *((int *)id);
- int end = start + 1;
- setbuf(fp, NULL);// 設(shè)置緩沖區(qū)的大小
- fprintf(stdout, "%d\n", start);
- for (int i = (start * 10); i < (end * 10); i ++){
- fprintf(fp, "%d\t", i);
- }
- fprintf(fp, "\n");
- fclose(fp);
- // 解鎖
- pthread_mutex_unlock(&mutex);
- return NULL;
- }
- int main(){
- int num_thread = 5;
- pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread);
- int * id = (int *)malloc(sizeof(int) * num_thread);
- // 初始化互斥鎖
- if (pthread_mutex_init(&mutex, NULL) != 0){
- // 互斥鎖初始化失敗
- free(pt);
- free(id);
- return 1;
- }
- for (int i = 0; i < num_thread; i++){
- id[i] = i;
- if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){
- printf("thread create failed!\n");
- return 1;
- }
- }
- for (int i = 0; i < num_thread; i++){
- pthread_join(pt[i], NULL);
- }
- pthread_mutex_destroy(&mutex);
- // 釋放資源
- free(pt);
- free(id);
- return 0;
- }
最終的結(jié)果為:
參考文章:
http://www.broadview.com.cn/article/297
https://www.cnblogs.com/jingzhishen/p/3807455.html
本文轉(zhuǎn)載自微信公眾號「一口Linux」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系一口Linux公眾號。