2012-05-16 64 views
2

我有2個線程來創建thread1和thread2。當創建線程:多線程啓動安排

pthread_create(&thread1, NULL, &function_th1, NULL); 
pthread_create(&thread2, NULL, &function_th2, NULL); 

的線程2在線程1推出之前,我正在尋找一個解決方案線程2之前啓動線程1。

不找這樣的解決方案:

pthread_create(&thread2, NULL, &function_th2, NULL); 
pthread_create(&thread1, NULL, &function_th1, NULL); 
+1

你可以發佈更多的背景,爲什麼你要對線程1線程2前推出? – Jay

回答

1

這裏的解決方案後,我建議

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

static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; 
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 
static bool wait = TRUE; 

void thread_sync() { 
    pthread_mutex_lock(&mut); 
    wait = FALSE; 
    pthread_cond_signal(&cond); 
    pthread_mutex_unlock(&mut); 
} 
void thread_wait_sync() { 
    pthread_mutex_lock(&mut); 
    if (wait==TRUE) 
    { 
     pthread_cond_wait(&cond,&mut); 
    } 
    wait = TRUE; 
    pthread_mutex_unlock(&mut); 
} 

void *thread1(void *d) { 
    thread_sync(); 
    while (1); // Rest of work happens whenever 
    return NULL; 
} 

void *thread2(void *d) { 
    thread_sync(); 
    while (1); 
    return NULL; 
} 

void *thread3(void *d) { 
    thread_sync(); 
    while (1); 
    return NULL; 
} 

void *thread4(void *d) { 
    while (1); 
    return NULL; 
} 

int main() { 
    pthread_t t1,t2,t3,t4; 
    pthread_create(&t1, NULL, thread1, NULL); 
    thread_wait_sync(); 
    pthread_create(&t2, NULL, thread2, NULL); 
    thread_wait_sync(); 
    pthread_create(&t3, NULL, thread3, NULL); 
    thread_wait_sync(); 
    pthread_create(&t4, NULL, thread4, NULL); 
    while(1) { 
    // some work 
    } 
} 
+0

我沒有得到這段代碼。你正在做'廣播'和'信號',但沒有人在等待? – Tudor

+0

抱歉,這是一個錯誤,我更新了代碼 – MOHAMED

+0

此代碼錯誤地使用條件變量。如果在主線程沒有等待時調用'pthread_cond_signal',則什麼都不會發生,並且您的代碼將會死鎖,等待一個永遠不會到達的信號。此外還需要標誌;以Tudor的答案爲例。 – jilles

5

有當一個線程創建的號召和線程實際開始執行時沒有關係。這一切都取決於實現,操作系統等,這就是爲什麼你看到兩個線程實際上以一個看似隨機的順序開始。

如果您需要線程1在線程2之前啓動,那麼您可能對某些事件有一些數據/邏輯依賴關係。在這種情況下,你應該使用一個條件變量來告訴線程2什麼時候開始執行。

// condition variable 
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 
// associated mutex 
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 
// state for condition variable 
int shouldWait = 1; 

... 

void* function_th1(void* p) { 
    // execute something 

    // signal thread 2 to proceed 
    pthread_mutex_lock(&mutex); 
    shouldWait = 0; 
    pthread_cond_signal(&cond); 
    pthread_mutex_unlock(&mutex); 

    // other stuff 
} 

void* function_th2(void* p) { 
    // wait for signal from thread 1 
    pthread_mutex_lock(&mutex); 
    while(shouldWait) { 
     pthread_cond_wait(&cond, &mutex); 
    } 
    pthread_mutex_unlock(&mutex); 

    // other stuff 
}  
+0

同時檢查或設置該條件時使用同步。互斥體將在這種情況下完成。 – superM

+0

@superM:我發佈了一些示例代碼。 – Tudor

+0

+1,特別是在改變條件和信號時正確鎖定,我見過很多人都忘記了。 – Hasturkun

3

移動 '在pthread_create(&線程2,NULL,& function_th2,NULL);'到'function_th1'函數的頂部。

這肯定會達到你所要求的,不需要複雜的信號。

您要求的是您真正想要或需要的是另一回事。

+0

嗯...因爲我沒有看到任何狀態傳遞這可能是一個好主意! – Tudor

+0

我已經試過這個,但行爲是隨機的。 – developer

+0

@Tudor你的解決方案很好,我已經認爲我可以優先運行我的線程。 Linux上是否存在這樣的解決方案?因爲我開發了一個多線程應用程序。 – developer