2015-12-10 34 views
0

這是我的代碼:子線程將錯誤的數據返回到主線程?

pthread_t client_thread1, client_thread2; 
int *child_thread_data = new int; 

pthread_mutex_t testlock; 

typedef struct 
{ 
    int thread_no; 
    char thread_name[100]; 
} thrdata; 

int main() 
{ 
    void *ret_data1 = new 
    int[10]; 
    void *ret_data2 = new 
    int[10]; 

    thrdata thr1; 
    thrdata thr2; 

    thr1.thread_no = 10; 
    strcpy((thr1.thread_name), "thread one"); 
    thr2.thread_no = 20; 
    strcpy((thr2.thread_name), "thread two"); 

    pthread_create(&client_thread1, &attr, &client_handler, &thr1); 
    pthread_create(&client_thread2, &attr, &client_handler, &thr2); 

    cout << "value of ret_data1 before thread 1 join " << *(int *) ret_data1 
     << endl; 
    cout << "value of ret_data2 before thread 2 join " << *(int *) ret_data2 
     << endl; 

    pthread_join(client_thread1, &ret_data1); 
    cout << "value returned by thread1 is " << *(int *) ret_data1 << endl; 

    pthread_join(client_thread2, &ret_data2); 
    cout << "value returned by thread2 is " << *(int *) ret_data2 << endl; 

    return 0; 
} 

void *client_handler(void *arg) 
{ 
    pthread_mutex_lock(&testlock); 

    thrdata *client_thd; 
    client_thd = (thrdata *) arg; 

    cout << "inside client_handler value of private data no is \n" 
     << client_thd->thread_no << endl; 
    cout << "inside client_handler value of private data name is \n" 
     << client_thd->thread_name << endl; 

    if (pthread_equal(client_thread1, pthread_self())) 
    { 
    *child_thread_data = 100; 
    pthread_mutex_unlock(&testlock); 
    pthread_exit((void *) &child_thread_data); 
    } 

    else if (pthread_equal(client_thread2, pthread_self())) 
    { 
    *child_thread_data = 200; 
    pthread_mutex_unlock(&testlock); 
    pthread_exit((void *) &child_thread_data); 
    } 
} 

輸出:

value of ret_data1 before thread 1 join 0 
value of ret_data2 before thread 2 join 0 


inside client_handler value of private data no is 20 
inside client_handler value of private data name is thread two 


calling pthread_exit of thread2 and value of child_thread_data is 200 


inside client_handler value of private data no is 10 
inside client_handler value of private data name is thread one 

calling pthread_exit of thread1 and value of child_thread_data is 100 

value returned by thread1 is 28262416 
value returned by thread2 is 28262416 

爲什麼兩個子線程返回相同的數據?我在pthread_exit(28262416)中設置了不同的數據,但我仍然在兩種情況下都收到相同的數據。請讓我知道原因。

謝謝。

+1

C中支持「new」嗎? –

回答

2

我看到兩個問題:首先這兩個線程都修改並返回相同的指針。其次,線程將指針返回給指針(例如int **)。

第一個問題只會給你相同的結果(但與你現在得到的結果無關)。第二個問題是什麼給你你現在得到的結果(這是變量child_thread_data在內存中的地址)。

要解決這兩個問題,首先需要兩個值來分配和返回,並返回一個指針(int *)。

噢,鎖並不重要,因爲最後運行的線程總是會覆蓋結果。

你也有內存泄漏,你分配內存並分配給ret_data1ret_data2然後你只需丟棄該內存時pthread_join覆蓋這些指針。

我的建議是你回去幾步,find a good book about C並從指針章節重新開始。並嘗試瞭解何時何地應該使用指針。因爲現在你在不需要的時候使用指針(例如,不需要child_thread_data作爲指針)。

+0

如果首先執行線程1,並且先調用pthread_join,然後執行thread2,則在這種情況下,printf將有兩個不同的值... –

+0

@JeegarPatel是的,但這是一個非常大的「if」。而且不太可能,因爲控制檯輸出很慢,變量'child_thread_data'在打印之前可能有時間被第二個線程修改。 –

+0

對......你的回答在所有情況下都是正確的。 +1 –