2014-01-26 35 views
0

我得到每個線程壞ID,我怎麼能得到一個正確的ID?而且我必須創造一個像元帥一樣的線索,允許副駕駛,我該如何解決這個問題?我正在使用講臺作爲監視器來鎖定或解鎖允許只能訪問一個線程的互斥鎖。多線程,身份證不工作,如何添加其他功能

我有這樣的代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <linux/sched.h> 

typedef enum 
{ false = 0, true } t_bool; 

pthread_mutex_t mutex; 
int rostrum; 
rostrum = 0; 

void * 
deputy (void *arg) 
{ 
    int tid = (int *) arg; 
    printf ("Deputy no: %d in f() \n", tid); // ??? no.1 

    int SAID; 
    SAID = 0; 

    while (SAID == 0) 
    { 
     pthread_mutex_trylock (&rostrum); 
     if (rostrum == 0) 
     { 
      rostrum = 1; 
      printf ("\t Deputy no: %d is saing\n", tid); 
      SAID = 1; 
     } 
     pthread_mutex_unlock (&rostrum); 
    } 
} 

int 
main() 
{ 
    pthread_t tid; 
    int i = 0; 
    for (i; i < 20; i++) 
    { 
     /* spurious characters deleted here */ 
     printf ("Deputy no: %d before f().\n", i); // ??? no.2 
     pthread_create (&tid, NULL, deputy, &tid); 
    } 
} 

結果:

Deputy no: 0 before f() 
    Deputy no: 1 before f() 
    Deputy no: 2 before f() 
    Deputy no: -863940960 inf f() 
    Deputy no: -863940960 inf f() 
Deputy no: -863940960 is saing 
Deputy no: -863940960 is saing 
    Deputy no: 3 before f() 
    Deputy no: -863940960 inf f() 
    Deputy no: 4 before f() 
    Deputy no: -863940960 inf f() 
Deputy no: -863940960 is saing 
    Deputy no: 5 before f() 
    Deputy no: -863940960 inf f() 
Deputy no: -863940960 is saing 
    Deputy no: 6 before f() 
    Deputy no: 7 before f() 
    Deputy no: -863940960 inf f() 
    Deputy no: 8 before f() 
    Deputy no: -863940960 inf f() 
Deputy no: -863940960 is saing 
    Deputy no: 9 before f() 
    Deputy no: 10 before f() 
    Deputy no: 11 before f() 
    Deputy no: -863940960 inf f() 
Deputy no: -863940960 is saing 
Deputy no: -863940960 is saing 
Deputy no: -863940960 is saing 
    Deputy no: 12 before f() 
    Deputy no: -863940960 inf f() 
    Deputy no: -863940960 inf f() 
Deputy no: -863940960 is saing 
    Deputy no: -863940960 inf f() 
    Deputy no: -863940960 inf f() 
Deputy no: -863940960 is saing 
    Deputy no: 13 before f() 
Deputy no: -863940960 is saing 
Deputy no: -863940960 is saing 
    Deputy no: 14 before f() 
    Deputy no: -863940960 inf f() 
    Deputy no: -863940960 inf f() 
Deputy no: -863940960 is saing 
Deputy no: -863940960 is saing 
    Deputy no: 15 before f() 
    Deputy no: -863940960 inf f() 
    Deputy no: 16 before f() 
    Deputy no: -863940960 inf f() 
Deputy no: -863940960 is saing 
Deputy no: -863940960 is saing 
    Deputy no: -863940960 inf f() 
    Deputy no: 17 before f() 
    Deputy no: 18 before f() 
    Deputy no: -863940960 inf f() 
    Deputy no: -863940960 inf f() 
Deputy no: -863940960 is saing 
    Deputy no: 19 before f() 
Deputy no: -863940960 is saing 
Deputy no: -863940960 is saing 
+0

爲什麼你將所有的線程指向一個tid var,無論如何,可能不再存在,因爲main()已經返回? –

回答

0

要從線程本身使用pthread_self()內取出一個線程的並行線程-ID。

爲此修改threas運作是這樣的:

void * 
deputy (void * arg) 
{ 
    pthread_t ptid = pthread_self(); 
    ... 

如果你真的要存儲在主線程向下傳遞它的引用創建的每個線程的並行線程-ID,您需要每個提供的變量,就像這樣:

int 
main (void) 
{ 
    pthread_t ptid[20]; 

    { 
    size_t i = 0; 
    for (; i < 20; ++i) 
    { 
     printf ("Deputy no: %zu before f().\n", i); 
     pthread_create (&ptid[i], NULL, deputy, &ptid[i]); 
    } 
    } 

    /* Make sure main does not end before all threads ended, 
    as they make use of ptid[] which lives on main's stack. */ 
    { 
    size_t i = 0; 
    for (; i < 20; ++i) 
    { 
     pthread_join(ptid[i]); 
    } 
    } 
} 

也請注意,通過倒pthread_t的地址輸入時變量,線程函數也應把它作爲這樣的,喜歡這裏:

void * 
deputy (void *arg) 
{ 
    pthread_t ptid = *((pthread_t *) arg); 
    ... 

而且^ 2 pthread_t是不透明的。所以它的表示是實現定義的。如果可以是一個整數或一個指針或... - 所以如果依賴於所使用的PThread庫的具體實現如何打印。

+0

它的工作,好的。但是我怎樣才能解決元帥的問題?請代理髮言? 贊: wantToSay(); listeningOther(); speaking(); 雖然(1) { invite_deputy [no]; //元帥邀請代理 } 與類Monitor – kxyz

+0

@kxyz:我是sry我必須說明我沒有得到您的要求。它們似乎與您所展示的代碼無關。 – alk

+0

我解決了這個問題,謝謝你的幫助:) – kxyz

1

在你main()你有

pthread_t tid; 

你不初始化,所以它的內容是不確定的。

您將指針傳遞給這個作爲你的論點在這裏pthread_create

pthread_create (&tid, NULL, deputy, &tid); 

我認爲你是希望你指定&tid作爲第一個參數來pthread_create將在tid覆蓋值。但是,這有兩個問題。首先,你不能保證什麼時候會發生 - printf可能已經運行在新線程中。第二種情況是,對pthread_create的後續調用可能會覆蓋此值,即您可以在執行第一個printf之前運行兩個pthread_create調用。

然後用這個作爲你線程的'id'。

int tid = (int *) arg; 
printf ("Deputy no: %d in f() \n", tid); // ??? no.1 

這實際上會在每個線程中打印相同的未定義值。

如果要獲取唯一的線程ID,請使用pthread_self,而不是查看傳遞的參數並嘗試將參數pthread_create的結果作爲參數傳遞。

相關問題