ホームページ > バックエンド開発 > C++ > スレッド同期を使用した番号の印刷順序

スレッド同期を使用した番号の印刷順序

王林
リリース: 2023-09-22 21:41:10
転載
1158 人が閲覧しました

スレッド同期を使用した番号の印刷順序

ここでは、さまざまなスレッドを使用して正しい順序で数値を出力する方法を見ていきます。ここでは、n 個のスレッドを作成し、それらを同期します。最初のスレッドが 1 を出力し、次に 2 番目のスレッドが 2 を出力する、という考え方です。 1 つのスレッドが印刷しようとすると、リソースがロックされ、他のスレッドがその部分を使用できなくなります。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t* cond = NULL;

int threads;

volatile int count = 0;

void* sync_thread(void* num) { //this function is used to synchronize the threads

   int thread_number = *(int*)num;

   while (1) {

      pthread_mutex_lock(&mutex); //lock the section

      if (thread_number != count) { //if the thread number is not same as count, put all thread

          except one into waiting state

         pthread_cond_wait(&cond[thread_number], &mutex);

      }

      printf("%d ", thread_number + 1); //print the thread number

         count = (count+1)%(threads);

      // notify the next thread

      pthread_cond_signal(&cond[count]);

      pthread_mutex_unlock(&mutex);

   }

   return NULL;

}

int main() {

   pthread_t* thread_id;

   volatile int i;

   int* thread_arr;

   printf("</p><p>Enter number of threads: ");

      scanf("%d", &threads);

   // allocate memory to cond (conditional variable) thread id&#39;s and array of size threads

   cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t) * threads);

   thread_id = (pthread_t*)malloc(sizeof(pthread_t) * threads);

   thread_arr = (int*)malloc(sizeof(int) * threads);

   for (i = 0; i < threads; i++) { //create threads

      thread_arr[i] = i;

      pthread_create(&thread_id[i], NULL, sync_thread, (void*)&thread_arr[i]);

   }

   // waiting for thread

   for (i = 0; i < threads; i++) {

      pthread_join(thread_id[i], NULL);

   }

   return 0;

}

ログイン後にコピー

出力

1

2

3

4

5

6

7

8

9

10

11

12

13

14

$ g++ test.cpp -lpthread

$ ./a.out

 

Enter number of threads: 5

1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3

4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3

4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1

2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4

5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2

3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

...

...

...

ログイン後にコピー

以上がスレッド同期を使用した番号の印刷順序の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート