2014-12-07 31 views
0

我真的是新來的線程,我必須做一個任務。我有一個6節點的圖,我應該創建從第一個節點移動到最後一個節點的線程。除了需要在線程中完成的部分之外,我已經設置了一切。我在網上看了一些教程,但它們還不夠,我遇到了一些問題,我不明白爲什麼。總之,這裏的代碼:pthreads的問題

int main (void) { 

    pthread_t threads[NUM_THREADS]; 
    int i = 0, rc, a = 0; 
    creatGraph(); 
    rc = pthread_create(&threads[i], NULL, buscarExp(i,0), NULL); 
    if (rc) { 
     printf("ERROR al crear el funcionari %d\n,",i); 
     exit(-1); 
    } 
    pthread_exit(NULL); 
    return 0; 
} 

這裏是我所需要的兩個主要rutines:

void buscarDespatx(int i, int actual) { 
     if(llista[actual].id == 0) { // ja hem arribat al despatx 
      pthread_exit(NULL); 
      //buscarExp(i,actual); // el funcionari ha deixat l'expedient i va a buscar-ne un altre 
     } else { // no ha arribat al despatx 
      int seguent = rand() % llista[actual].Npares; // trio el node pare de tots els nodes pares q tindra 
      llista[actual].Proces[llista[actual].membres] = 0; // trec el proces del node actual 
      llista[actual].membres--; // decremento el nombre de processos al noded actual 
      llista[llista[actual].pares[seguent]].membres++; // incremento el nombre de processos del node pare al que anira el proces actual 
      llista[llista[actual].pares[seguent]].Proces[llista[llista[actual].pares[seguent]].membres] = i; // afegeixo el proces actual a la llista de processos del node pare que anira el proces 
      buscarDespatx(i,llista[llista[actual].pares[seguent]].id); 
      printf("BUSCAR EXP: El node %d ha estat modificat i ha marxat el proces %d\n",actual,i); 
     } 
    } 

void buscarExp(int i, int actual) { 
    if(llista[actual].id == -1) { // ja hem arribat al expedient 
     buscarDespatx(i,actual); // el funcionari te l'expedient i el va a deixar al despatx 
    } else { // no ha arribat a l'expedient 
     int node = 0; 
     if(llista[actual].dret != NULL) { // aquest node te dos fils 
      int seg = rand(); 
      if(seg%2 == 0) { // avança pel fill esq 
       llista[actual].esq->membres++; // sumo un membre al seguent node 
       llista[actual].esq->Proces[llista[actual].esq->membres] = i; // poso el proces al seguent node 
      } else { // avanço pel fill dret 
       node = 1; 
       llista[actual].dret->membres++; // sumo un membre al seguent node 
       llista[actual].dret->Proces[llista[actual].dret->membres] = i; // poso el proces al seguent node 
      } 
     } else { // nomes te fill esquerra 
      llista[actual].esq->membres++; // sumo un membre al seguent node 
      llista[actual].esq->Proces[llista[actual].esq->membres] = i; // poso el proces al seguent node 
     } 
     llista[actual].Proces[llista[actual].membres] = 0;  // elimino el funcionari del node actual 
     llista[actual].membres--; 
     printf("BUSCAR EXP: El node %d ha estat modificat i ha marxat el proces %d\n",actual,i); 
     if(node == 1) { // ha passat pel dret 
      buscarExp(i,llista[actual].dret->id); 
     } else { // passa per l'esquerra 
      buscarExp(i,llista[actual].esq->id); 
     } 
    } 
} 

所以,如果我瞭解線程是如何工作的,這應該做到以下幾點: 「主」創建pthread並開始執行路由「buscarExp(i,0)」,然後在「buscarExp(i,0)」中繼續執行此例程,直到它到達圖的底部if(llista[actual].id == -1),然後返回到例程爲「buscarDespatx(i,actual)」的第一個節點。當它初始化節點時,我使用pthread_exit(NULL);來終止該線程。

這是如何在我主要創建的線程行爲如果代碼是100%正確的?

謝謝!

+0

' 「它不工作」'是非常不夠的。什麼不起作用?你看到你沒有想到的是什麼?您是否添加了調試打印消息?你是否已經在調試器中完成了你的代碼? – 2014-12-07 16:16:28

+0

你是對的...我知道我應該更具體,我試圖確認是否理解線程的流程,我將編輯主帖 – 2014-12-07 16:35:55

回答

2
rc = pthread_create(&threads[i], NULL, buscarExp(i,0), NULL); 

不工作。如果您查看pthread_create的文檔,您會看到第三個參數(start_routine)應該是一個函數指針。但是,您的代碼是第一個調用buscarExp(i,0),然後嘗試將該(錯誤鍵入的)結果作爲線程函數傳遞給pthread_create

您需要將具有兼容簽名的功能傳遞給pthread_create,並使用void*向其傳遞任何其他參數。

此外,您的主線程正在退出,這是一個問題,因爲我通過的數據生活在main()的堆棧上。最有可能的是,在你啓動它們後,你想要pthread_join與你的所有線程。

事情是這樣的:

struct thread_data { 
    int thread_num; 
    // Other things you want to pass here 
} 

void *thread_func(void *_data) { 
    struct thread_data *data = _data; 

    buscarExp(data->thread_num, 0) 
} 

int main (void) { 

    pthread_t threads[NUM_THREADS]; 
    struct thread_data thread_data[NUM_THREADS]; 

    int i = 0, rc, a = 0; 

    creatGraph(); 

    /* Start all threads */ 
    for (i=0; i<NUM_THREADS; ++i) { 
     thread_data[0].thread_num = i; 
     rc = pthread_create(&threads[i], NULL, thread_func, &thread_data[i]); 
     if (rc) { 
      printf("ERROR al crear el funcionari %d\n,",i); 
      exit(-1); 
     } 
    } 

    /* Wait for all threads to finish */ 
    for (i=0; i<NUM_THREADS; ++i) { 
     pthread_join(&threads[i], NULL); 
    } 
    return 0; 
} 
+0

好的,這是我一直在尋找。謝謝!最後一個問題是,如果我理解你的代碼是正確的,這將創建NUM_THREADS並在它們中的每一個上運行buscarExp,當它們完成buscarExp時,它們將完成並且一切都會結束,對吧? – 2014-12-08 13:50:49

+1

是的。我認爲這是你的代碼的標題。 – 2014-12-08 14:19:10

+0

好吧我完全理解(我認爲)pthreads是如何工作的,謝謝Jonathon! – 2014-12-08 14:40:06