信号量等待案例

时间:2012-12-27 14:04:38

标签: c pthreads semaphore

我想确保我的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;
}

终端〜&GT; ./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 _  

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

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

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

希望这有帮助。