ホームページ  >  記事  >  バックエンド開発  >  スレッド同期に C プログラムを使用して数値を順番に出力する

スレッド同期に C プログラムを使用して数値を順番に出力する

王林
王林転載
2023-08-26 22:45:061327ブラウズ

スレッド同期に C プログラムを使用して数値を順番に出力する

スレッドを指定すると、プログラムは優先順位に従ってスレッドを 0 から 10 まで出力する必要があります。

スレッドとは何ですか?

スレッドは、プログラム内で実行される軽量のプロセスです。単純なプログラムには n 個のスレッドを含めることができます。

Java とは異なり、C/C 言語標準はマルチスレッドをサポートしていません。C/C のマルチスレッドの標準は POSIX スレッド (Pthread) です。 C 言語にはマルチスレッド アプリケーションのサポートが組み込まれておらず、この機能の提供はオペレーティング システムに完全に依存しています。私たちのプログラムでは

はどのように機能しますか?

スレッド関数を使用するには、ヘッダー ファイル #include を使用します。このヘッダー ファイルには、pthread_create() など、プログラム内のすべてのスレッド関連関数が含まれます。

現在のタスクは、gcc コンパイラーによって提供される pthread 標準ライブラリを使用して、n 個のスレッドを同期することです。このアイデアは、スレッド数を取得し、最初のスレッドに 1、2 番目のスレッドに 2、3 番目のスレッドに 3 を、10 番目のスレッドまで出力することです。出力には、スレッドの優先順位に基づいて 1 から 10 までの数字が含まれます。

Algorithm

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

の中国語訳は次のとおりです:

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

Output

上記のプログラムを実行すると、次の出力が生成されます

1 2 3 4 5 6 7 8 9 10

以上がスレッド同期に C プログラムを使用して数値を順番に出力するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。