0

我想确保我的 sempahore 做我期望做的事情,但我不能把它带到一个或多个线程等待的状态。我一次只需要 3 个线程就可以处理链表。

代码:

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

struct dataBlock{
    struct node *root;
    int listSize;
    int forIndex;
};

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

int limit = 5;

sem_t sem;

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

void *deleteDoneNodes(struct node *n){
    struct node *root = n;
    struct node *it = root;
    struct node *prev = NULL;
    do{
        if(it->value == 1){
            struct node *next = it->next;
            if (prev != NULL) {
                prev->next = next;
            }
            if (it == root) {
                root = next;
            }
            free(it);
            it = next;
        }
        else {
            prev = it;
            it = it->next;
        }
    }while(it !=  NULL);

    return root;
}

void * worker( void *data ){
    //get list
    int wFlag;
    struct dataBlock *inData = ( struct dataBlock * ) data;
    struct node *root = inData->root;
    int forIndex = inData ->forIndex;
    free(data);


    while(1){

        if( sem_wait( &sem )  != 0 ){
            printf( " > waiting...  \n" );
        }
        // pthread_mutex_lock( &mutp );
        struct node *it = root;

        do{
            if( forIndex == it->worker ){
                if( it->value > 2 ){
                    while( it->value != 1 )
                    it->value = sqrt(it->value);
                }
            }
            else{
                // printf("Not sqrt-able node %d\n",it->value);
            }
            it = it->next;
        }while(it !=  NULL);

        // pthread_cond_signal( &condvar ); 
        // pthread_mutex_unlock( &mutp );
        sem_post(&sem); 
        // sleep(100); // "create" concurrancy envi.
        pthread_exit(0);    
    }

    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;
    struct node *root;
    struct node *iterator;  

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

    root->value = rand() % 100;
    root->worker = 0;
    iterator = root;

    for( i=1; i<listSize; i++ ){
        iterator->next = malloc(sizeof(struct node));
        iterator = iterator->next;
        iterator->value = rand() % 100;
        iterator->worker = i % nrWorkers;
        printf("node #%d worker: %d  value: %d\n", i, iterator->worker,iterator->value);
    }
    iterator->next = NULL;
    printf("? List got populated\n");
// init semaphore > keeps max 3 threads working over the list

    if( sem_init(&sem,0,3) < 0){
      perror("semaphore initilization");
      exit(0);
    }

// Create all threads to parse the link list
    int ret;    
    pthread_mutex_init(&mutp,NULL);

    pthread_t w_thread;
    pthread_t* w_threads = malloc(nrWorkers * sizeof(w_thread));

    for( i=0; i < nrWorkers; i++ ){         
        struct dataBlock *data = malloc(sizeof(struct dataBlock));
        data->root = root;
        data->listSize = listSize;
        data->forIndex = i;
        ret = pthread_create ( &w_threads[i], NULL, worker, (void *) data );
        if( ret ) {
            perror("Thread creation fail");
            exit(2);    
        }   
    } 

    deleteDoneNodes( root );

    int join;
    for ( i = 0; i < nrWorkers; i++ ){
        join = pthread_join(w_threads[i],NULL);
    }

    iterator = root;
    for ( i = 0; i < listSize; i++){
        printf("val: %d  worker: %d _  \n", iterator->value, iterator->worker);
        iterator = iterator->next;
    }

    free(root);
    free(iterator);
    sem_destroy(&sem);
    return 0;
}

终端~> ./s 16 16

node #1 worker: 1  value: 86
node #2 worker: 2  value: 77
node #3 worker: 3  value: 15
node #4 worker: 4  value: 93
node #5 worker: 5  value: 35
node #6 worker: 6  value: 86
node #7 worker: 7  value: 92
node #8 worker: 8  value: 49
node #9 worker: 9  value: 21
node #10 worker: 10  value: 62
node #11 worker: 11  value: 27
node #12 worker: 12  value: 90
node #13 worker: 13  value: 59
node #14 worker: 14  value: 63
node #15 worker: 15  value: 26
? List got populated
val: 1  worker: 0 _  
val: 1  worker: 1 _  
val: 1  worker: 2 _  
val: 1  worker: 3 _  
val: 1  worker: 4 _  
val: 1  worker: 5 _  
val: 1  worker: 6 _  
val: 1  worker: 7 _  
val: 1  worker: 8 _  
val: 1  worker: 9 _  
val: 1  worker: 10 _  
val: 1  worker: 11 _  
val: 1  worker: 12 _  
val: 1  worker: 13 _  
val: 1  worker: 14 _  
val: 1  worker: 15 _  
4

2 回答 2

0

使用来自 glib "http://developer.gnome.org/glib/stable/glib-Thread-Pools.html" 的线程池

于 2012-12-27T14:21:42.487 回答
0

我可以在您的代码中看到一些潜在的错误。

首先,我认为您的批评区是所有节点,因此您应该使用互斥锁保护所有访问。在您的工人中,您没有这样做。

但是,您的信号量应该可以工作:在线程函数的开头使用 3 和 sem_wait 初始化,最后使用 sem_post 初始化。

所以,我能想到的最大问题是你的 deletenodes 函数。不要忘记 main 也将是一个线程,并且由于您在连接之前删除节点并且发送到线程的数据是用指针构造的,如果您的所有线程在处理数据之前都丢失了处理器,并且您的主线程获取处理器,它将清除所有节点,使您的数据类型,传递给工作线程,过时的,空的,最好的情况下为空。就像我说的那样,发生这种情况是因为您通过指针/引用而不是通过复制将值传递给数据。我可以看到两种可能的解决方案:要么在加入后调用删除节点(将主线程状态设置为阻塞,断言您对删除节点的调用在所有线程结束后发生),要么通过复制将值传递给数据(至在我看来,将

希望这可以帮助。

于 2012-12-27T17:07:05.310 回答