首頁 > 後端開發 > C++ > 主體

使用C程式進行執行緒同步,依序列印數字

王林
發布: 2023-08-26 22:45:06
轉載
1352 人瀏覽過

使用C程式進行執行緒同步,依序列印數字

給定線程,程式必須根據它們的優先權從0到10列印線程。

什麼是執行緒?

執行緒是在程式內部運行的輕量級進程。一個簡單的程式可以包含n個執行緒。

與Java不同,C/C 語言標準不支援多線程,POSIX線程(Pthreads)是C/C 中多線程的標準。 C語言不包含任何內建的多執行緒應用程式支持,而是完全依賴作業系統來提供此功能。

在我們的程式中如何運作?

為了使用執行緒函數,我們使用頭檔#include。這個頭檔將包含我們程式中與執行緒相關的所有函數,如pthread_create()等。

現在的任務是使用gcc編譯器提供的pthread標準函式庫來同步n個執行緒。思路是獲取線程計數並在第一個線程中打印1,在第二個線程中打印2,在第三個線程中打印3,直到第十個線程。輸出將根據執行緒的優先權包含從1到10的數字。

演算法

Start
Step 1 -> Declare global variables as int MAX=10 and count=1
Step 2 -> declare variable thr of pthread_mutex_t and cond of pthread_cond_t
Step 3 -> Declare Function void *even(void *arg)
   Loop While(count < MAX)
      Call pthread_mutex_lock(&thr)
      Loop While(count % 2 != 0)
         Call pthread_cond_wait(&cond, &thr)
      End
      Print count++
      Call pthread_mutex_unlock(&thr)
      Call pthread_cond_signal(&cond)
   End
   Call pthread_exit(0)
Step 4 -> Declare Function void *odd(void *arg)
   Loop While(count < MAX)
      Call pthread_mutex_lock(&thr)
      Loop While(count % 2 != 1)
         Call pthread_cond_wait(&cond, &thr)
      End
      Print count++
      Call pthread_mutex_unlock(&thr)
      Call pthread_cond_signal(&cond)
   End
   Set pthread_exit(0)
Step 5 -> In main()
   Create pthread_t thread1 and pthread_t thread2
   Call pthread_mutex_init(&thr, 0)
   Call pthread_cond_init(&cond, 0)
   Call pthread_create(&thread1, 0, &even, NULL)
   Call pthread_create(&thread2, 0, &odd, NULL)
   Call pthread_join(thread1, 0)
   Call pthread_join(thread2, 0)
   Call pthread_mutex_destroy(&thr)
   Call pthread_cond_destroy(&cond)
Stop
登入後複製

Example

的中文翻譯為:

範例

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int MAX = 10;
int count = 1;
pthread_mutex_t thr;
pthread_cond_t cond;
void *even(void *arg){
   while(count < MAX) {
      pthread_mutex_lock(&thr);
      while(count % 2 != 0) {
         pthread_cond_wait(&cond, &thr);
      }
      printf("%d ", count++);
      pthread_mutex_unlock(&thr);
      pthread_cond_signal(&cond);
   }
   pthread_exit(0);
}
void *odd(void *arg){
   while(count < MAX) {
      pthread_mutex_lock(&thr);
      while(count % 2 != 1) {
         pthread_cond_wait(&cond, &thr);
      }
      printf("%d ", count++);
      pthread_mutex_unlock(&thr);
      pthread_cond_signal(&cond);
   }
   pthread_exit(0);
}
int main(){
   pthread_t thread1;
   pthread_t thread2;
   pthread_mutex_init(&thr, 0);
   pthread_cond_init(&cond, 0);
   pthread_create(&thread1, 0, &even, NULL);
   pthread_create(&thread2, 0, &odd, NULL);
   pthread_join(thread1, 0);
   pthread_join(thread2, 0);
   pthread_mutex_destroy(&thr);
   pthread_cond_destroy(&cond);
   return 0;
}
登入後複製

輸出

如果我們執行上述程序,它將生成以下輸出

1 2 3 4 5 6 7 8 9 10
登入後複製

以上是使用C程式進行執行緒同步,依序列印數字的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!