2015-01-07 60 views
2

我正在嘗試動態創建pthread並面對變量尋址中的問題。能否請你告訴的地址應如何訪問創建動態pthread_t時出錯

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

+1

什麼是錯誤? –

+0

你可能想要選擇一種語言。如果你選擇C++,你可以使用'std :: thread',這很容易。 – MSalters

回答

4

在pthread_create()需要pthread_t*類型作爲第一個參數。你有pthread_t陣列,使得通過其中之一的地址:

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

還要注意的是投(void *)t是不正確的。你應該傳遞一個指向有效對象的指針。

+0

謝謝先生。在使用指針時我非常糟糕 – user4428032