2012-11-09 136 views
3

我試圖從pthread_join打印返回值。我有以下代碼:C pthread_join返回值

for(j = 0 ; j < i ; ++j){ 
     pthread_join(tid[j], returnValue); /* BLOCK */ 
     printf("%d\n", (int)&&returnValue); 
} 

所有線程都存儲在tid數組中,並且被正確創建並返回。在每個線程函數的末尾,我有以下行:

pthread_exit((void *)buf.st_size); 

我正在嘗試返回某些正在讀取的文件的大小。出於某種原因,我無法打印出正確的值。這更可能是我試圖從pthread_join函數調用中取消引用void **的方式,但我不太確定如何去做。預先感謝您的幫助。

回答

7

你需要一個void *變量的地址傳遞給pthread_join - 它會被與退出值填入:

for(j = 0 ; j < i ; ++j) { 
    void *returnValue; 
    pthread_join(tid[j], &returnValue); /* BLOCK */ 
    printf("%d\n", (int)returnValue); 
} 
+0

這使得很多感覺,我現在唯一的問題是我編譯它時收到的警告,「警告:從指針轉換爲不同大小的整數[-Wpointer-to-int-cast]」,除此之外它按照我的需要工作至。 –

+0

使用'intptr_t'而不是'int'來獲取與指針大小相同的整數類型。 –

+0

最後一行應該是'printf(「%d \ n」,*(int *)returnValue);' – smac89

0

這是工作:

for(j = 0 ; j < i ; ++j) { 
    int returnValue; 
    pthread_join(tid[j], (void **)&returnValue); /* BLOCK */ 
    printf("%d\n", returnValue); 
}