0
我正在編寫一個代碼,它創建10個線程,並首先使用偶數線程ID執行這些線程,然後再執行所有使用奇數線程ID的線程。我正在使用POSIX線程庫。這是我寫的代碼:多線程程序不能生成所需的輸出
#include "stdlib.h"
#include "pthread.h"
#include "stdio.h"
#define TRUE 1
#define FALSE 0
int EVEN_DONE = FALSE;
int evenThreads, oddThreads = 0;
int currentThread = 0;
//the mutex for thread synchronization
static pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
//the condition variable;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void * printEven(unsigned long id)
{
pthread_mutex_lock(&mymutex);
evenThreads++;
printf("TID: %lu, Hello from even\n", id);
// this condition checks whether even threads have finished executing
if(evenThreads + oddThreads >= 10) {
EVEN_DONE = TRUE;
pthread_cond_broadcast(&cond);
}
pthread_mutex_unlock(&mymutex);
return NULL;
}
void * printOdd(unsigned long id)
{
pthread_mutex_lock(&mymutex);
while (!EVEN_DONE) {
oddThreads++;
pthread_cond_wait(&cond, &mymutex);
printf("TID: %lu, Hello from odd\n", id);
}
pthread_mutex_unlock(&mymutex);
return NULL;
}
void * threadFunc(void *arg)
{
unsigned long id = (unsigned long)pthread_self();
if (id % 2 == 0)
{
printEven(id);
}
else
{
printOdd(id);
}
return NULL;
}
int main()
{
pthread_t* threads;
int num_threads = 10;
int i, j;
threads = malloc(num_threads * sizeof(threads));
for (i = 0; i < 10; i++) {
pthread_create(&threads[i], NULL, threadFunc, NULL);
}
for (j = 0; j < 10; j++) {
pthread_join(threads[j], NULL);
}
printf("Finished executing all threads\n");
return 0;
}
然而,當我運行它不會產生期望的輸出代碼。我得到的輸出是這樣的:
顯然,這似乎所有的線程ID爲偶數。不過,我認爲我的代碼存在問題。我究竟做錯了什麼?我怎樣才能達到理想的輸出?
(注:我在入門級的水平是當它涉及到POSIX線程和多線程一般)提前
感謝。
沒有理由(我知道)爲什麼pthreads庫不能爲其線程ID選擇偶數。事實上,如果pthreads庫將其線程ID基於一個指針值,那麼我希望它們都是,因爲指針通常是字對齊的。 –
如果是這樣的話,我怎麼才能測試代碼的正確性? – avidProgrammer
如果是這種情況,您需要退後一步並更正程序的假設,而不是程序本身。例如,放棄對線程ID值的假設。相反,在父線程中生成值並將每個值作爲參數傳遞給每個線程。 – kaylum