日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解Linux之線程互斥鎖

在編程中,引入了對(duì)象互斥鎖的概念,來保證共享數(shù)據(jù)操作的完整性。每個(gè)對(duì)象都對(duì)應(yīng)于一個(gè)可稱為” 互斥鎖” 的標(biāo)記,這個(gè)標(biāo)記用來保證在任一時(shí)刻,只能有一個(gè)線程訪問該對(duì)象。Linux實(shí)現(xiàn)的互斥鎖機(jī)制包括POSIX互斥鎖和內(nèi)核互斥鎖,本文主要講POSIX互斥鎖,即線程間互斥鎖。

信號(hào)量用在多線程多任務(wù)同步的,一個(gè)線程完成了某一個(gè)動(dòng)作就通過信號(hào)量告訴別的線程,別的線程再進(jìn)行某些動(dòng)作(大家都在sem_wait的時(shí)候,就阻塞在 那里)。而互斥鎖是用在多線程多任務(wù)互斥的,一個(gè)線程占用了某一個(gè)資源,那么別的線程就無法訪問,直到這個(gè)線程unlock,其他的線程才開始可以利用這 個(gè)資源。比如對(duì)全局變量的訪問,有時(shí)要加鎖,操作完了,在解鎖。有的時(shí)候鎖和信號(hào)量會(huì)同時(shí)使用的”

也就是說,信號(hào)量不一定是鎖定某一個(gè)資源,而是 流程上的概念,比如:有A,B兩個(gè)線程,B線程要等A線程完成某一任務(wù)以后再進(jìn)行自己下面的步驟,這個(gè)任務(wù)并不一定是鎖定某一資源,還可以是進(jìn)行一些計(jì)算 或者數(shù)據(jù)處理之類。而線程互斥量則是“鎖住某一資源”的概念,在鎖定期間內(nèi),其他線程無法對(duì)被保護(hù)的數(shù)據(jù)進(jìn)行操作。在有些情況下兩者可以互換。

兩者之間的區(qū)別:

作用域

信號(hào)量 : 進(jìn)程間或線程間(linux僅線程間)

互斥鎖 : 線程間

上鎖時(shí)

信號(hào)量 : 只要信號(hào)量的value大于0,其他線程就可以sem_wait成功,成功后信號(hào)量的value減一。若value值不大于0,則sem_wait阻塞,直到sem_post釋放后value值加一。一句話,信號(hào)量的value>=0 。

互斥鎖 : 只要被鎖住,其他任何線程都不可以訪問被保護(hù)的資源。如果沒有鎖,獲得資源成功,否則進(jìn)行阻塞等待資源可用。一句話,線程互斥鎖的vlaue可以為負(fù)數(shù) 。

多線程

線程是計(jì)算機(jī)中獨(dú)立運(yùn)行的最小單位,運(yùn)行時(shí)占用很少的系統(tǒng)資源。與多進(jìn)程相比,多進(jìn)程具有多進(jìn)程不具備的一些優(yōu)點(diǎn),其最重要的是:對(duì)于多線程來說,其能夠比多進(jìn)程更加節(jié)省資源。

線程創(chuàng)建

在Linux中,新建的線程并不是在原先的進(jìn)程中,而是系統(tǒng)通過一個(gè)系統(tǒng)調(diào)用clone()。該系統(tǒng)copy了一個(gè)和原先進(jìn)程完全一樣的進(jìn)程,并在這個(gè)進(jìn)程中執(zhí)行線程函數(shù)。

在Linux中,通過函數(shù)pthread_create()函數(shù)實(shí)現(xiàn)線程的創(chuàng)建:

pthread_create()

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*st

其中:

thread表示的是一個(gè)pthread_t類型的指針;

attr用于指定線程的一些屬性;

start_routine表示的是一個(gè)函數(shù)指針,該函數(shù)是線程調(diào)用函數(shù);

arg表示的是傳遞給線程調(diào)用函數(shù)的參數(shù)。

當(dāng)線程創(chuàng)建成功時(shí),函數(shù)pthread_create()返回0,若返回值不為0則表示創(chuàng)建線程失敗。對(duì)于線程的屬性,則在結(jié)構(gòu)體pthread_attr_t中定義。

線程創(chuàng)建的過程如下所示:

#include  
#include  
#include  
#include  

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 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()用于將主進(jìn)程處于等待狀態(tài),以讓線程執(zhí)行完成。最終的執(zhí)行效果如下所示:

Linux之線程互斥鎖Linux之線程互斥鎖

那么,如何利用arg向子線程傳遞參數(shù)呢?其具體的實(shí)現(xiàn)如下所示:

#include  
#include  
#include  
#include  

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 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í)行效果如下圖所示:

Linux之線程互斥鎖Linux之線程互斥鎖

如果在主進(jìn)程提前結(jié)束,會(huì)出現(xiàn)什么情況呢?如下述的代碼:

#include  
#include  
#include  
#include  

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 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;
}

此時(shí),主進(jìn)程提前結(jié)束,進(jìn)程會(huì)將資源回收,此時(shí),線程都將退出執(zhí)行,運(yùn)行結(jié)果如下所示:

Linux之線程互斥鎖Linux之線程互斥鎖

線程掛起

在上述的實(shí)現(xiàn)過程中,為了使得主線程能夠等待每一個(gè)子線程執(zhí)行完成后再退出,使用了free()函數(shù),在Linux的多線程中,也可以使用pthread_join()函數(shù)用于等待其他線程,函數(shù)的具體形式為:

int pthread_join(pthread_t thread, void **retval);

函數(shù)pthread_join()用來等待一個(gè)線程的結(jié)束,其調(diào)用這將被掛起。

一個(gè)線程僅允許一個(gè)線程使用pthread_join()等待它的終止。

如需要在主線程中等待每一個(gè)子線程的結(jié)束,如下述代碼所示:

#include  
#include  
#include  
#include  

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 if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){
        printf("thread create failed!\n");
        return 1;
      }
  }
  for (int i = 0; i return 0;
}

最終的執(zhí)行效果如下所示:

Linux之線程互斥鎖Linux之線程互斥鎖

注:在編譯的時(shí)候需要鏈接libpthread.a:

g++ xx.c -lpthread -o xx

互斥鎖mutex

多線程的問題引入

多線程的最大的特點(diǎn)是資源的共享,但是,當(dāng)多個(gè)線程同時(shí)去操作(同時(shí)去改變)一個(gè)臨界資源時(shí),會(huì)破壞臨界資源。如利用多線程同時(shí)寫一個(gè)文件:

#include  
#include  

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 "%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 if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){
        printf("thread create failed!\n");
        return 1;
        }
  }
  for (int i = 0; i return 0;
}

執(zhí)行以上的代碼,我們會(huì)發(fā)現(xiàn),得到的結(jié)果是混亂的,出現(xiàn)上述的最主要的原因是,我們?cè)诰帉懚嗑€程代碼的過程中,每一個(gè)線程都嘗試去寫同一個(gè)文件,這樣便出現(xiàn)了上述的問題,這便是共享資源的同步問題,在Linux編程中,線程同步的處理方法包括:信號(hào)量,互斥鎖和條件變量。

互斥鎖

互斥鎖是通過鎖的機(jī)制來實(shí)現(xiàn)線程間的同步問題。互斥鎖的基本流程為:

初始化一個(gè)互斥鎖:pthread_mutex_init()函數(shù)

加鎖:pthread_mutex_lock()函數(shù)或者pthread_mutex_trylock()函數(shù)

對(duì)共享資源的操作

解鎖:pthread_mutex_unlock()函數(shù)

注銷互斥鎖:pthread_mutex_destory()函數(shù)

其中,在加鎖過程中,pthread_mutex_lock()函數(shù)和pthread_mutex_trylock()函數(shù)的過程略有不同:

當(dāng)使用pthread_mutex_lock()函數(shù)進(jìn)行加鎖時(shí),若此時(shí)已經(jīng)被鎖,則嘗試加鎖的線程會(huì)被阻塞,直到互斥鎖被其他線程釋放,當(dāng)pthread_mutex_lock()函數(shù)有返回值時(shí),說明加鎖成功;

而使用pthread_mutex_trylock()函數(shù)進(jìn)行加鎖時(shí),若此時(shí)已經(jīng)被鎖,則會(huì)返回EBUSY的錯(cuò)誤碼。

同時(shí),解鎖的過程中,也需要滿足兩個(gè)條件:

解鎖前,互斥鎖必須處于鎖定狀態(tài);

必須由加鎖的線程進(jìn)行解鎖。

當(dāng)互斥鎖使用完成后,必須進(jìn)行清除。

有了以上的準(zhǔn)備,我們重新實(shí)現(xiàn)上述的多線程寫操作,其實(shí)現(xiàn)代碼如下所示:

#include  
#include  

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 "%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 if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){
        printf("thread create failed!\n");
        return 1;
     }
  }
  for (int i = 0; i return 0;
}

最終的結(jié)果為:

Linux之線程互斥鎖Linux之線程互斥鎖


本文題目:詳解Linux之線程互斥鎖
URL標(biāo)題:http://www.5511xx.com/article/cdesihi.html