亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

理解線程2 信號量和互斥量 理解線程同步

Honwhy / 2488人閱讀

摘要:線程同步了解線程信號量的基礎(chǔ)知識,對深入理解的線程會大有幫助。當(dāng)兩個(gè)線程同時(shí)執(zhí)行時(shí),不可避免同時(shí)操作同一個(gè)變量或者文件等,所以需要有一組機(jī)制來確保他們能正確的運(yùn)行信號量和互斥量。

線程同步

了解線程信號量的基礎(chǔ)知識,對深入理解python的線程會大有幫助。

當(dāng)兩個(gè)線程同時(shí)執(zhí)行時(shí),不可避免同時(shí)操作同一個(gè)變量或者文件等,所以需要有一組機(jī)制來確保他們能正確的運(yùn)行:信號量和互斥量。信號量可以分為最簡單的“二進(jìn)制信號量”和更通用的“計(jì)數(shù)信號量”。信號量通常用來保護(hù)一段代碼,使其每次只能被一個(gè)執(zhí)行線程運(yùn)行,這種情況下需要用到二進(jìn)制信號量。有時(shí)候希望可以允許有限數(shù)目的線程執(zhí)行一段指定代碼,這就需要用到計(jì)數(shù)信號量。實(shí)際上,技術(shù)信號量是一種二進(jìn)制信號量的邏輯擴(kuò)展,實(shí)際兩者調(diào)用的函數(shù)一樣。

互斥量和信號量很相似,事實(shí)上他們可以互相通過對方來實(shí)現(xiàn)。但在實(shí)際應(yīng)用中,對于一些情況使用其中一種更符合語義而且效果更好。

用信號量進(jìn)行同步
#include 
#include 
#include 
#include 
#include 
#include 

void *thread_function(void *arg);
sem_t bin_sem;

#define WORK_SIZE 1024
char work_area[WORK_SIZE];      /* 用來存放輸入內(nèi)容 */

int main() {
  int res;                    /* 暫存一些命令的返回結(jié)果 */
  pthread_t a_thread;         /* 織帶新建的線程 */
  void *thread_result;       /* 存放線程處理結(jié)果 */

  res = sem_init(&bin_sem, 0, 0);   /* 初始化信號量,并且設(shè)置初始值為0*/
  if (res != 0) {
    perror("Semaphore initialization failed");
    exit(EXIT_FAILURE);
  }
  res = pthread_create(&a_thread, NULL, thread_function, NULL);   /* 創(chuàng)建新線程 */
  if (res != 0) {
    perror("Thread creation failed");
    exit(EXIT_FAILURE);
  }
  printf("Inout some text, Enter "end" to finish
");
  while(strncmp("end", work_area, 3) != 0) {             /* 當(dāng)工作區(qū)內(nèi)不是以end開頭的字符串時(shí)...*/
    fgets(work_area, WORK_SIZE, stdin);                  /* 從標(biāo)準(zhǔn)輸入獲取輸入到worl_area */
    sem_post(&bin_sem);                                  /* 信號量+1 */
  }
  printf("
Waiting for thread to finish...
");
  res = pthread_join(a_thread, &thread_result);         /* 等待線程結(jié)束 */
  if (res != 0) {
    perror("Thread join failed");
    exit(EXIT_FAILURE);
  }
  printf("Thread joined
");
  sem_destroy(&bin_sem);                               /* 銷毀信號量 */
  exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
  sem_wait(&bin_sem);                                 /* 等待信號量有大于0的值然后-1 */
  while(strncmp("end", work_area, 3) != 0) {
    printf("You input %ld characters
", strlen(work_area)-1);   /* 獲取輸入字符串長度 8*/
    sem_wait(&bin_sem);                               /* 等待信號量有大于0的值然后-1 */
  }
  pthread_exit(NULL);
}
用互斥量進(jìn)行同步
#include 
#include 
#include 
#include 
#include 
#include 

void *thread_function(void *arg);
pthread_mutex_t work_mutex;

#define WORK_SIZE 1024
char work_area[WORK_SIZE];
int time_to_exit = 0;                           /* 用來控制TODO*/

int main() {
  int res;
  pthread_t a_thread;
  void *thread_result;

  res = pthread_mutex_init(&work_mutex,NULL);    /* 初始化一個(gè)互斥鎖 */
  if (res != 0) {
    perror("Mutex initialization failed");
    exit(EXIT_FAILURE);
  }
  res = pthread_create(&a_thread, NULL, thread_function, NULL);  /* 創(chuàng)建一個(gè)新線程 */
  if (res != 0) {
    perror("Thread creation failed");
    exit(EXIT_FAILURE);
  }
  pthread_mutex_lock(&work_mutex);                       /* 嘗試對互斥量加鎖 */
  printf("Input some text, Enter "end" to finish
");
  while(!time_to_exit) {                                   /* 檢查是不是該退出*/
    fgets(work_area, WORK_SIZE, stdin);                   /* 從標(biāo)準(zhǔn)輸入獲取輸入到work_area */
    pthread_mutex_unlock(&work_mutex);                   /* 解鎖互斥量 */
    while(1) {
      pthread_mutex_lock(&work_mutex);
      if (work_area[0] != "