【发布时间】:2015-01-07 08:42:34
【问题描述】:
我正在尝试动态创建 pthread 并面临变量寻址问题。你能告诉我应该如何访问这个地址吗
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);
}
错误:lvalue required as unary ‘&’ operand
【问题讨论】:
-
您可能想选择一种语言。如果你选择 C++,你可以使用
std::thread,这样会容易很多。
标签: c++ c multithreading memory-address