2011-02-16 182 views
0

實際上,主要的場景是:從主線程有兩個線程正在運行。通過使用條件變量,兩個線程將運行並休眠,然後它將返回到主線程。我的意思是我不想要不同的輸出模式。只有一種模式:從main-> thread1-> thread2-> main。 我已經寫了對C thread.It代碼顯示我有時候有時候想not.as例如,輸出的是結果:一個線程如何在另一個線程中被殺死

I am in thread 1 
before conditional wait 
I am in thread 2 
before conditional release 
i am again in thread 2 
i am again in thread 1 
main exits here 

的問題是有時「這裏主要退出」不execute.Please幫助me.It要注意的是,我不能使用在pthread_join()。我的代碼如下

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <semaphore.h> 

pthread_mutex_t gLock; 
pthread_cond_t gCondition; 

pthread_mutex_t mLock; 
pthread_cond_t mCondition; 

void initialize() 
{ 
     pthread_mutex_init(&gLock, NULL); 
     pthread_cond_init (&gCondition, NULL); 
     pthread_mutex_init(&mLock, NULL); 
     pthread_cond_init (&mCondition, NULL); 

     return; 
} 

void * threadOne(void * msg) 
{ 
    printf("%s \n",(char*) msg); 
    printf("before conditional wait\n"); 

    pthread_mutex_lock(&gLock); 
    pthread_cond_wait(&gCondition,&gLock); 
    pthread_mutex_unlock(&gLock); 

    printf("i am again in thread 1\n"); 

    pthread_mutex_lock(&mLock); 
    pthread_cond_signal(&mCondition); 
    pthread_mutex_unlock(&mLock); 

} 

void * threadTwo(void * msg) 
{ 
    printf("%s\n",(char*)msg); 
    printf("before conditional release\n"); 
    pthread_mutex_lock(&gLock); 
    pthread_cond_signal(&gCondition); 
    pthread_mutex_unlock(&gLock); 
    printf("i am again in thread 2\n"); 

} 

int main() 
{ 
     pthread_t thread1; 
     pthread_t thread2; 

     char * msg1="I am in thread 1"; 
     char * msg2="I am in thread 2"; 
     initialize(); 

     pthread_create(&thread1,NULL,threadOne,(void*) msg1); 
     pthread_create(&thread2,NULL,threadTwo,(void*) msg2); 

     pthread_mutex_lock(&mLock); 
     pthread_cond_wait(&mCondition,&mLock); 
     pthread_mutex_unlock(&mLock); 

     printf("main exits here"); 

     return 0; 
} 
+0

爲什麼不能使用pthread_join?這是父線程等待孩子完成的常用方式。 – paxdiablo 2011-02-16 08:45:57

回答

0

給出的問題是,你不正確使用的條件變量。條件變量只是一個通知機制,而不是一個標誌。除了當前正在等待的線程列表之外,它沒有內部狀態。因此,如果main()在其他線程調用pthread_cond_signal()時沒有實際執行遠達pthread_cond_wait()的調用,那麼信號將丟失,並且main()將永遠等待。

您需要使用與條件變量關聯的單獨標誌。然後main()可以檢查這個標誌,並且只有等待標誌沒有被設置。此外,它必須在循環中檢查該標誌,以確保處理「虛假喚醒」,其中pthread_cond_wait()返回而不是對應的信號。 threadOnethreadTwo之間的通知同樣適用。

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <semaphore.h> 

pthread_mutex_t gLock; 
pthread_cond_t gCondition; 
int gFlag=0; 

pthread_mutex_t mLock; 
pthread_cond_t mCondition; 
int mFlag=0; 

void initialize() 
{ 
    pthread_mutex_init(&gLock, NULL); 
    pthread_cond_init (&gCondition, NULL); 
    pthread_mutex_init(&mLock, NULL); 
    pthread_cond_init (&mCondition, NULL); 
} 

void * threadOne(void * msg) 
{ 
    printf("%s \n",(char*) msg); 
    printf("before conditional wait\n"); 

    pthread_mutex_lock(&gLock); 
    while(!gFlag) 
    { 
     pthread_cond_wait(&gCondition,&gLock); 
    } 
    pthread_mutex_unlock(&gLock); 

    printf("i am again in thread 1\n"); 

    pthread_mutex_lock(&mLock); 
    mFlag=1; 
    pthread_cond_signal(&mCondition); 
    pthread_mutex_unlock(&mLock); 

} 

void * threadTwo(void * msg) 
{ 
    printf("%s\n",(char*)msg); 
    printf("before conditional release\n"); 
    pthread_mutex_lock(&gLock); 
    gFlag=1; 
    pthread_cond_signal(&gCondition); 
    pthread_mutex_unlock(&gLock); 
    printf("i am again in thread 2\n"); 

} 

int main() 
{ 
    pthread_t thread1; 
    pthread_t thread2; 

    char * msg1="I am in thread 1"; 
    char * msg2="I am in thread 2"; 
    initialize(); 

    pthread_create(&thread1,NULL,threadOne,(void*) msg1); 
    pthread_create(&thread2,NULL,threadTwo,(void*) msg2); 

    pthread_mutex_lock(&mLock); 
    while(!mFlag) 
    { 
     pthread_cond_wait(&mCondition,&mLock); 
    } 
    pthread_mutex_unlock(&mLock); 

    printf("main exits here"); 

    return 0; 
}