简体   繁体   English

创建动态pthread_t时出错

[英]Error when creating dynamic pthread_t

I am trying to dynamically create pthread and facing the issue in addressing of the variable. 我正在尝试动态创建pthread并在解决变量时面临问题。 Can you please tell how the address should be accessed 你能告诉我该如何访问地址吗

int main (int argc, char *argv[])
{
   pthread_t *threads;
   int rc, numberOfThreads;
   long t;
   cout<<"Number of Threads = ";
   cin>>numberOfThreads;
   cout<<endl;
   threads =(pthread_t*) malloc(numberOfThreads*sizeof(pthread_t));
   for(t=0; t<numberOfThreads; t++){
      printf("In main: creating thread %ld\n", t);
     // **ERROR ON BELOW LINE**
      rc = pthread_create((pthread_t)&(threads+numberOfThreads), NULL, FunctionForThread, (void *)t);
      (void) pthread_join(threads[t], NULL);

      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }

   /* Last thing that main() should do */
   pthread_exit(NULL);
}

ERROR : lvalue required as unary '&' operand 错误lvalue required as unary '&' operand

pthread_create() requires a pthread_t* type as the first parameter. pthread_create()需要pthread_t*类型作为第一个参数。 You have an array of pthread_t so pass an address of one of them: 您有一个pthread_t数组,因此请传递其中一个的地址:

rc = pthread_create(&threads[i], NULL, FunctionForThread, (void *)t);

Also note that the cast (void *)t is not correct. 另请注意,强制转换(void *)t不正确。 You should pass a pointer to a valid object. 您应该将指针传递给有效对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM