PThread基本程序,好奇这段代码有什么问题

时间:2012-11-30 22:27:43

标签: c pthreads

为了编写这段代码,我检查并使用了一些教程,仍然在某个地方我失败了,考虑到只有主要功能似乎工作并退出代码0.是不是我的踏板被创建了?但他们不知道我将它们与我的“工作”功能联系起来了吗?

#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);
    }       
}

Ps 一些评论的函数/变量将被使用/实现,但此时我期待线程

1 个答案:

答案 0 :(得分:2)

我怀疑你在创建线程的for循环中应该有:

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

另外,我建议你为输入参数添加检查/打印。

还有一个问题:你将指针传递给worker(),所以你必须取消引用指针,如果它指向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;
    ...

相反,您只需在一行代码中分配数组,而无需为 root 分配:

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

如您所见,此处不需要下一个指针。