2012-04-24 86 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