歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 信號量和互斥量C語言示例理解線程同步

信號量和互斥量C語言示例理解線程同步

日期:2017/3/1 9:08:54   编辑:Linux編程

Table of Contents

  • 1. 線程同步
    • 1.1. 用信號量進行同步
    • 1.2. 用互斥量進行同步
  • 2. 參考資料

線程同步

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

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

互斥量和信號量很相似,事實上他們可以互相通過對方來實現。但在實際應用中,對於一些情況使用其中一種更符合語義而且效果更好。

用信號量進行同步

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>

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

#define WORK_SIZE 1024
char work_area[WORK_SIZE];      /* 用來存放輸入內容 */

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

  res = sem_init(&bin_sem, 0, 0);   /* 初始化信號量,並且設置初始值為0*/
  if (res != 0) {
    perror("Semaphore initialization failed");
    exit(EXIT_FAILURE);
  }
  res = pthread_create(&a_thread, NULL, thread_function, NULL);   /* 創建新線程 */
  if (res != 0) {
    perror("Thread creation failed");
    exit(EXIT_FAILURE);
  }
  printf("Inout some text, Enter 'end' to finish\n");
  while(strncmp("end", work_area, 3) != 0) {             /* 當工作區內不是以end開頭的字符串時...*/
    fgets(work_area, WORK_SIZE, stdin);                  /* 從標准輸入獲取輸入到worl_area */
    sem_post(&bin_sem);                                  /* 信號量+1 */
  }
  printf("\nWaiting for thread to finish...\n");
  res = pthread_join(a_thread, &thread_result);         /* 等待線程結束 */
  if (res != 0) {
    perror("Thread join failed");
    exit(EXIT_FAILURE);
  }
  printf("Thread joined\n");
  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\n", strlen(work_area)-1);   /* 獲取輸入字符串長度 8*/
    sem_wait(&bin_sem);                               /* 等待信號量有大於0的值然後-1 */
  }
  pthread_exit(NULL);
}

用互斥量進行同步

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>

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

#define WORK_SIZE 1024
char work_area[WORK_SIZE];
int time_to_exit = 0;                           /* 用來控制是否退出*/

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

  res = pthread_mutex_init(&work_mutex,NULL);    /* 初始化一個互斥鎖 */
  if (res != 0) {
    perror("Mutex initialization failed");
    exit(EXIT_FAILURE);
  }
  res = pthread_create(&a_thread, NULL, thread_function, NULL);  /* 創建一個新線程 */
  if (res != 0) {
    perror("Thread creation failed");
    exit(EXIT_FAILURE);
  }
  pthread_mutex_lock(&work_mutex);                       /* 嘗試對互斥量加鎖 */
  printf("Input some text, Enter 'end' to finish\n");
  while(!time_to_exit) {                                   /* 檢查是不是該退出*/
    fgets(work_area, WORK_SIZE, stdin);                   /* 從標准輸入獲取輸入到work_area */
    pthread_mutex_unlock(&work_mutex);                   /* 解鎖互斥量 */
    while(1) {
      pthread_mutex_lock(&work_mutex);
      if (work_area[0] != '\0') {                      /* 持續檢查work_area 是否為空, 如果不為空繼續等待,如果為空,則重新讀取輸入到work_area*/
        pthread_mutex_unlock(&work_mutex);
        sleep(1);
      }
      else {
        break;
      }
    }
  }
  pthread_mutex_unlock(&work_mutex);
  printf("\nWaiting for thread to finish...\n");
  res = pthread_join(a_thread, &thread_result);
  if (res != 0) {
    perror("Thread join failed");
    exit(EXIT_FAILURE);
  }
  printf("Thread joined\n");
  pthread_mutex_destroy(&work_mutex);
  exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
  sleep(1);
  pthread_mutex_lock(&work_mutex);                     /* 嘗試加鎖互斥量 */
  while(strncmp("end", work_area, 3) != 0) {           /* 當work_area裡的值不是以end開頭時*/
    printf("You input %ld characters\n", strlen(work_area) -1);     /* 輸出輸入的字符長度 */
    work_area[0] = '\0';                                      /* work_area設置為空 */
    pthread_mutex_unlock(&work_mutex);
    sleep(1);
    pthread_mutex_lock(&work_mutex);
    while (work_area[0] == '\0') {              /* 持續檢查work_area 直到它裡面有輸入值*/
      pthread_mutex_unlock(&work_mutex);
      sleep(1);
      pthread_mutex_lock(&work_mutex);
    }
  }
  time_to_exit = 1;                        /* 當輸入end後,設置退出標志 */
  work_area[0] = '\0';
  pthread_mutex_unlock(&work_mutex);
  pthread_exit(0);
}

參考資料

  • 《Beginning Linux Programming》

Copyright © Linux教程網 All Rights Reserved