挂起sem_wait()

时间:2013-03-18 13:06:15

标签: c linux multithreading hardware netfilter

我有一些多线程应用程序。这是主要功能的一部分:

/* ...some code before... */
for(i=0; i<THREADS_COUNT; i++){
    status = pthread_create(&threads[i], NULL, thread_main, NULL);
    if(status < 0){
        fprintf(stderr, "threads error\n");
        exit(2);
    }
}
status = sem_init(&sem, 0, 0);
if(status < 0){
        fprintf(stderr, "sem_init error\n");
        exit(4);
}
/* recv loop */
while (1) {
    rv = recv(fd, buf, BUFSIZE, 0);
    if(rv >= 0){
        current = malloc(sizeof(struct queue_msg_list));
        /* adding to our local queue */
        if(current != NULL){
            current->rv = rv;
            current->h = h;
            memcpy(&(current->buf), &buf, BUFSIZE);
            current->next = NULL;
            if(main_head == NULL){
                main_head = main_tail = current; 
            } 
            else {
                main_tail->next = current;
                main_tail = current;
            }
            count++;
        }
        /* if we can carry the local queue to the queue for threads then we are doing it */
        if(!pthread_mutex_trylock(&mlock)){
            if(thread_head == NULL){
                /* if the threads-queue is empty then replace queues */
                thread_head = main_head;
                thread_tail = main_tail;
            } else {
                /* if the threads-queue is not empty then add the local queue to the threads-queue */                   
                thread_tail->next = main_head;
                thread_tail = main_tail;
            }
            /* we increasing a semaphore of number of added elements */
            for(i=0; i<count; i++){
                sem_post(&sem);
                printf("sem_post \n");
            }
            count = 0;
            pthread_mutex_unlock(&mlock);
            main_head = NULL;
            main_tail = NULL;
        }
    }
}
/* ...some code after... */

这是线程的一个功能:

void *thread_main(void *arg)
{
    struct  queue_msg_list *current;
    char buf[BUFSIZE] __attribute__ ((aligned));
    struct nfq_handle *h;
    int rv;
    while(1){
        sem_wait(&sem);
        pthread_mutex_lock(&mlock);
        /* if no blocking then we are working with packet and removing it from list after */
        current = thread_head;
        rv = current->rv;
        h = current->h;
        memcpy(&buf, &(current->buf), BUFSIZE);
        thread_head = thread_head->next;
        pthread_mutex_unlock(&mlock);
        nfq_handle_packet(h, buf, rv);
        free(current);
    }
}

此应用程序始终适用于PC。我把这个应用程序放到一些路由器(固件中的Linux内核版本是2.6.30)。它有时可以正常工作,但有时它工作不正确。 :)

线程在有时会挂起sem_wait(&sem);,但信号量值大于零(1,2,3等)。为什么呢?

P.S。我试图检查sem_wait(&sem);的返回值,但没有得到它。

0 个答案:

没有答案