2012-11-30 34 views
0

爲了編寫這段代碼,我檢查並使用了一些教程,仍然在某處我失敗了,考慮到只有主函數似乎可以工作並且退出代碼0。我的腳步創造了?他們是但不知何故,我沒有把他們連接到我的「工作」功能?PThread基本程序,好奇這個代碼有什麼問題

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <unistd.h> 
#include <math.h> 


struct node { // std linked list node 
    int value; 
    int worker; 
    struct node *next; 
}; 
// pthread_t* w_thread; 

int slots = 3; // only 3 threads are allowed to access the list 
int availableCheck(){ // check if thread can acces the list 
    if(slots < 3) return 0; 
    else return -1; 
} 

pthread_mutex_t mutp = PTHREAD_MUTEX_INITIALIZER; //condvar mutex 
pthread_cond_t condvar = PTHREAD_COND_INITIALIZER; //condvar 

void * worker(void *i){ 
    long index = (long)i; 
    printf(" * I am worker # %lu : \n",pthread_self()); 
    // pthread_mutex_lock(&mutp); 
    // if(availableCheck() < 0){ 
     // printf(" ^^^ List not available yet... \n"); 
     // pthread_cond_wait(&condvar, &mutp); 
    printf("* Got data: %lu \n", index); 
    // pthread_cond_signal(&condvar); // 
    // pthread_mutex_unlock(&mutp); 

    return NULL; 
    // } 
} 

int main(int argc, char *argv[]){ 
    if (argc != 3){ 
     printf("Programm must be called with \n NR of elements and NR of workers! \n "); 
     exit(1); 
    } 

    int i,listSize,workersSize; 
    struct node *root; 
    struct node *iterator; 

//prepare list for task 
    listSize = atoi(argv[1]); 
    root = malloc(sizeof(struct node)); 

    root->value = rand() % 100; 
    // printf(">>> %d\n", root->value); 
    root->worker = 0; 

    iterator = malloc(sizeof(struct node)); 
    iterator = root; 

    for(i=1; i<listSize; i++){ 
     iterator->next = malloc(sizeof(struct node)); 
     iterator = iterator->next; 
     iterator->value = rand() % 100; 
     iterator->worker = i; 
     printf("node #%d worker: %d value: %d\n", i, iterator->worker,iterator->value); 
    } 
    printf("? List got populated\n"); 

// Create all threads to parse the link list 
    int ret;  
    pthread_t w_thread; 
    int nrWorkers = atoi(argv[2]); 
    pthread_t* w_threads = malloc(nrWorkers * sizeof(w_thread)); 
    for(i=0; i<listSize; i++){ 
     int *id = malloc(sizeof(int)); 
     *id = i; 
     ret = pthread_create (&w_threads[i], NULL, worker, (void *) &id); 
     if(ret) { 
      perror("Thread creation fail"); 
      exit(2);  
     } 
    } 
    int j; 
    for (j = 0; i < nrWorkers; j++){ 
     pthread_join(w_threads[j],NULL); 
    }  
} 

一些評論功能/變量將要使用/執行,但在這一點上我希望線程

+0

喲如何運行你的程序,以及在控制檯中打印什麼? – pmod

+0

'iterator = root;'爲什麼在malloc'ing之後覆蓋'iterator'? – 2012-11-30 22:37:31

+0

能夠迭代從根開始的列表 –

回答

2

我懷疑,在for循環中,要創建線程,你應該有:

for(i=0; i<nWorkers; i++){ 

此外,我建議你添加檢查/打印輸入參數。

而且多了一個問題:你通過指針工人(),所以你必須去參考指針,如果它的指針爲int,然後

void * worker(void *p_int){ 
    assert(p_int != NULL) 
    int index = *(int*)p_ind; 

是準確的從虛空鑄造*!

還有最後一件事。如果你知道LISTSIZE,目前還不清楚我爲什麼你想創建鏈接的列表:

for(i=1; i<listSize; i++){ 
    iterator->next = malloc(sizeof(struct node)); 
    iterator = iterator->next; 
    ... 

相反,你可以分配你的陣列只是一個代碼行,並不需要分配

p_list = (struct node*)malloc(listSize*sizeof(struct node)); 
assert(p_list); 
for(i=0; i<listSize; i++){ 
    p_list[i].value = ... 
    ... 

正如你看到的,你不需要下一個這裏指針。

+0

我必須使用鏈表:\ –