简体   繁体   中英

pthread and accessing the critical section

Well, the below code is for two threads. I have some problem with the mutex. After thread t1 is created, it calls add_queue(). Then it will signal the thread t2 to work in its critical section. But, the program does not run for thread t2. Thread t1's runs and does its work in its critical section. Then, I have locked the mutex for thread t2. But, the program gets stuck in the line 29.

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

static int pending_requests =0;
static pthread_mutex_t prmutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t prcondition = PTHREAD_COND_INITIALIZER;

static int critical = 0;

int get_number_requests(void)
{
    return pending_requests;
}

void add_queue()
{
    pthread_mutex_lock(&prmutex);
    pending_requests++;
    critical++;    
    pthread_mutex_unlock(&prmutex);
    printf("xxxx\n");
    printf("critical=%d\n",critical);

}

void remove_from_queue()
{
    pthread_mutex_lock(&prmutex);
    pending_requests--;
    printf("BBBBcritical=%d\n",critical);
    critical--;
    printf("BBBcritical=%d\n",critical);

    pthread_mutex_unlock(&prmutex);

}

void *get_request()
{
    add_queue();
    if (get_number_requests() != 0)
    {    
        printf("I have a element in the queue. Signalling to processor thread..\n");
        pthread_cond_signal(&prcondition);
    }
    pthread_exit(0);
}

void *processor()
{
    while(get_number_requests() == 0)
    {
        printf("BBB This is a processor thread, I am waiting..\n");
        pthread_cond_wait(&prcondition,&prmutex);
    }
    while(get_number_requests() !=0)
    {
        printf("aaaa\n");
        remove_from_queue();
        pthread_cond_signal(&prcondition);
    }

    pthread_exit(0);
}


int main()
{
    pthread_t t1,t2;
    printf("critical=%d\n",critical);
    pthread_create(&t1,NULL,get_request,NULL);
    pthread_create(&t2,NULL,processor,NULL);
    printf("critical=%d\n",critical);

    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
}

Your use of pthread_cond_wait is wrong in a number of ways. Careful reading of the man page for pthread_cond_wait will help you understand how to use it properly.

  1. Before calling pthread_cond_wait the mutex must be locked. From the manual:

    They shall be called with mutex locked by the calling thread or undefined behavior results.

    That is, you need to call pthread_mutex_lock before calling pthread_cond_wait .

  2. When pthread_cond_wait returns it will have the mutex locked. From the manual:

    Upon successful return, the mutex shall have been locked and shall be owned by the calling thread.

    So it is wrong to attempt to lock it again in remove_from_queue . Since the mutex is already locked, the pthread_mutex_lock call in remove_from_queue will block indefinitely as you are finding (ie, the thread has deadlocked itself).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM