2012-04-24 84 views
0

編譯時,當我嘗試編譯此我得到一個錯誤:在pthread_create - 錯誤在C

warning: passing argument 1 of pthread_create makes pointer from integer without a cast.

請如果有人可以幫助我..

int Traveler(int id, int numBags) 
    { 
      int i; 
      int err; 
     err = pthread_create(id, NULL, Traveler, NULL); 
     if(err!=0) 
     { 
         return -1; 
     } 
     else 
     { 

     } 
    } 

回答

4

錯誤是相當清楚的。第一個參數應該是一個指針而不是一個整數。

人並行線程:

int pthread_create(pthread_t *restrict thread, 
      const pthread_attr_t *restrict attr, 
      void *(*start_routine)(void*), void *restrict arg); 

    The pthread_create() function shall create a new thread, with 
    attributes specified by attr, within a process. If attr is NULL, the 
    default attributes shall be used. If the attributes specified by attr 
    are modified later, the thread's attributes shall not be affected. Upon 
    successful completion, pthread_create() shall store the ID of the cre- 
    ated thread in the location referenced by thread. 

重新讀取最後一句你在pthread_create調用id之前堅持一個符號之前。 EDIT2:你還必須將id定義爲pthread_t。

編輯:

其實,有在同一行中另外兩個問題:需要的start_routine是一個函數,它有一個參數,而不是兩個,但最糟糕的這一切都將是一個fork bomb因爲你傳遞你打來的功能相同!

+1

+1,但在'id'前加'&'不是正確的做法 - 第一個參數需要是指向「pthread_t」的指針,而不是指向「int」的指針。編譯器可能會接受它(如果'pthread_t'恰好是'int'的類型定義),但通常不會。 – 2012-04-24 16:58:20

+0

好的。已經更新了我的答案。好吧,我想這會在一行中產生4個錯誤... :-) – smocking 2012-04-24 17:15:59