我是Linux中併發和線程概念的新手,我試圖解決一個相對簡單的問題。我創建了兩個線程,它們運行增加一個全局變量的相同函數。我真的從我的節目要的是交替增加變量,即在每個說一步遞增的變量打印到屏幕上一個消息的線程,因此預期的輸出應該是這樣的:Linux中的併發:在臨界區域交替訪問兩個線程
Thread 1 is incrementing variable count... count = 1
Thread 2 is incrementing variable count... count = 2
Thread 1 is incrementing variable count... count = 3
Thread 2 is incrementing variable count... count = 4
等上。
我試圖確保互斥信號量的實現,但儘管如此其結果類似於此:
Thread 2 is incrementing variable count... count = 1
Thread 2 is incrementing variable count... count = 2
Thread 2 is incrementing variable count... count = 3
Thread 2 is incrementing variable count... count = 4
Thread 2 is incrementing variable count... count = 5
Thread 1 is incrementing variable count... count = 6
Thread 1 is incrementing variable count... count = 7
Thread 1 is incrementing variable count... count = 8
Thread 1 is incrementing variable count... count = 9
Thread 1 is incrementing variable count... count = 10
我的代碼如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
int count = 0;
sem_t mutex;
void function1(void *arg)
{
int i = 0;
int *a = (int*) arg;
while (i < 10)
{
sem_wait(&mutex);
count++;
i++;
printf("From the function : %d count is %d\n",*a,count);
sem_post(&mutex);
}
}
int main()
{
pthread_t t1,t2;
int a = 1;
int b = 2;
pthread_create(&t1,NULL,(void *)function1,&a);
pthread_create(&t2,NULL,(void *)function1,&b);
sem_init(&mutex,0,1);
pthread_join(t2,NULL);
pthread_join(t1,NULL);
sem_destroy(&mutex);
return 0;
}
我的大問題是,現在,怎麼辦我在線程之間實現了這種交替?我得到了相互排斥,但這種改變依然不見了。也許我對信號量的使用缺乏洞察力,但如果有人會向我解釋這一點,我將非常感激。 (我已經閱讀了關於這個主題的幾門課程,即Linux信號量,併發和線程,但這些信息還不夠令人滿意)
我已經在我的IDE編寫的代碼,它在第一次打印後實際上會阻塞 – Dragos2795
因此,我不認爲它實際上是阻塞的,可能發生的情況是第一個線程持續獲得互斥鎖。因此,如果您要在if語句之外添加一個說明「線程* a正在運行」的打印,您應該看到它不斷打印出來。那會發生什麼? – Almog