简体   繁体   中英

printf with pthreads in C

I am working with pthreads right now doing the producer/consumer problem. I am currently just trying to get the producer working and using printf statements to see where my issues are. The problem is the code compiles just fine but when I run it, it doesn't do anything but seems to run just fine. I have tried setting my first line to a printf statement but even that does not print. I have tried using fflush as well and I am running out of ideas. My question why would even the first printf statement get skipped?

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

void *producer();

pthread_mutex_t lock;
pthread_cond_t done, full, empty;
int buffer[10];
int in = 0, out = 0;
int min = 0, max = 0, numOfItems = 0, total = 0;
double avg;


void *producer() {
    srand(time(NULL));
    int n = rand();
    int i;
    for(i = 0; i < n; i++)
    {
        int random = rand();
        pthread_mutex_lock(&lock);
        buffer[in++] = random;
        if(in == 10)
        {
            pthread_cond_signal(&full);
            printf("Buffer full");
            pthread_mutex_unlock(&lock);
            sleep(1);
        }
    }
    pthread_exit(NULL);
}

void *consumer() {
    pthread_exit(NULL);
}
int main(int argc, char *argv[]){
    printf("test");
    //Create threads and attribute
    pthread_t ptid, ctid;
    pthread_attr_t attr;

    //Initialize conditions and mutex
    pthread_cond_init(&full, NULL);
    pthread_cond_init(&empty, NULL);
    pthread_cond_init(&done, NULL);
    pthread_mutex_init(&lock, NULL);

    //Create joinable state
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    pthread_create(&ptid, &attr,(void *)producer,NULL);
    pthread_create(&ctid, &attr,(void *)consumer,NULL);


    pthread_join(ptid,NULL);
    pthread_join(ctid,NULL);

    printf("Program Finished!");
    pthread_exit(NULL);
}
man pthread_mutex_init

pthread_mutex_init initializes the mutex object pointed to by mutex according to the mutex attributes specified in mutexattr . If mutexattr is NULL , default attributes are used instead.

The LinuxThreads implementation supports only one mutex attributes, the mutex kind ... The kind of a mutex determines whether it can be locked again by a thread that already owns it. The default kind is fast ...

If the mutex is already locked by the calling thread, the behavior of pthread_mutex_lock depends on the kind of the mutex. If the mutex is of the fast kind, the calling thread is suspended until the mutex is unlocked, thus effectively causing the calling thread to deadlock.

That's what happens to your producer: it deadlocks in the call

    pthread_mutex_lock(&lock);

- except in the unlikely case n < 2 - thus producing no output.

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