迅雷筆試題:
編寫一個程序,開啟3個線程,這3個線程的ID分別為A、B、C,每個線程將自己的ID在屏幕上打印10遍,要求輸出結果必須按ABC的順序顯示;如:ABCABC….依次遞推。
程序:
- #include <pthread.h>
- #include <cstdio>
-
- const int THREAD_NUMBER = 3;
-
- // 子線程的互斥量和條件變量
- pthread_mutex_t thread_mutex[THREAD_NUMBER];
- pthread_cond_t thread_cond[THREAD_NUMBER];
-
- // 子線程是否正在等待
- bool thread_wait_flag[THREAD_NUMBER];
-
- // 標識輪到哪個子線程輸出其ID
- pthread_mutex_t mutex;
- int thread_turn;
-
- void *thread_func(void *arg);
-
- int main(int argc, char **argv)
- {
- pthread_t tids[THREAD_NUMBER];
-
- for (int i = 0; i < THREAD_NUMBER; ++i)
- {
- pthread_mutex_init(&thread_mutex[i], NULL);
- pthread_cond_init(&thread_cond[i], NULL);
- }
-
- pthread_mutex_init(&mutex, NULL);
- thread_turn = 0;
-
- for (int i = 0; i < THREAD_NUMBER; ++i)
- thread_wait_flag[i] = false;
-
- for (int i = 0; i < THREAD_NUMBER; ++i)
- {
- pthread_create(&tids[i], NULL, thread_func, (void *)i);
- }
-
- for (int i = 0; i < THREAD_NUMBER; ++i)
- {
- pthread_join(tids[i], NULL);
- }
- printf("\n");
- return 0;
- }
-
- void *thread_func(void *arg)
- {
- int id = (int)arg;
- char ch = 'A' + id;
- int count = 0;
-
- while (true)
- {
- if (id == thread_turn) // 若輪到當前子線程,輸出ID,發送信號
- {
- printf("%c", ch);
- ++count;
- if (id == THREAD_NUMBER-1 && count == 10) // 若是第3個子線程,輸出ID後,可直接退出。
- break;
- pthread_mutex_lock(&mutex);
- ++thread_turn;
- thread_turn %= THREAD_NUMBER;
- pthread_mutex_unlock(&mutex);
-
- while (true)
- {
- pthread_mutex_lock(&thread_mutex[thread_turn]);
- if (true == thread_wait_flag[thread_turn])
- {
- pthread_cond_signal(&thread_cond[thread_turn]);
- pthread_mutex_unlock(&thread_mutex[thread_turn]);
- break;
- }
- pthread_mutex_unlock(&thread_mutex[thread_turn]);
- }
- if (count == 10) // 若是第1、2個子線程,發出信號後,退出
- break;
- }
- else // 否則,等待
- {
- pthread_mutex_lock(&thread_mutex[id]);
- thread_wait_flag[id] = true;
- pthread_cond_wait(&thread_cond[id], &thread_mutex[id]);
- thread_wait_flag[id] = false;
- pthread_mutex_unlock(&thread_mutex[id]);
- }
- }
- return (void *)0;
- }