Consumer producer implementation involving POSIX, semaphores, and locks I am trying to get over my fear of multithreading programming and teaching myself to code using POSIX. I wrote a small version of the consumer producer problem. I am hoping I can get some feedback if it works (I think it does, as I traced the threads when my for loop was at 20 and it made sense) and what I should do to improve my code.  

The semaphore starts at 4, the consumer waits one second, and then consumes a semaphore.  The producer waits between 0 and 5 seconds, then produces (increments) the semaphore value. The consumer should wait if the semaphore is at 0 until the producer frees up the resources.

#include &lt;stdio.h&gt;
#include &lt;semaphore.h&gt;
#include &lt;pthread.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;

sem_t my_semaphore;
pthread_mutex_t count_mutex;
void * consumerUse(void * ptr);
void * producerUse(void * ptr);

int main() {

    int value;
    pthread_t consumer, producer;
    int valConsumer =0;
    int valProducer =0;

    sem_init(&amp;my_semaphore, 0, 4);
    sem_getvalue(&amp;my_semaphore, &amp;value);
    printf("The initial value of the semaphore is %d\n", value);

    for(int i=0; i &lt; 100; i++)
    {

        valConsumer = pthread_create(&amp; consumer, NULL, consumerUse, (void*)(intptr_t) value);
        if(valConsumer != 0)
        {
            fprintf(stderr, "Error - pthread create () return code: %d\n", valConsumer);
            exit(EXIT_FAILURE);
        }

        valProducer = pthread_create(&amp; producer, NULL, producerUse, (void*) (intptr_t)value);
        if(valProducer != 0)
        {
            fprintf(stderr, "Error - pthread create () return code: %d\n", valProducer);
            exit(EXIT_FAILURE);
        }

    }
    pthread_join( consumer, NULL);
    pthread_join( producer, NULL);
    printf("End program\n");
    return 0;
}

void *consumerUse(void * ptr)
{
    sleep(1);
    int value;
    sem_getvalue(&amp;my_semaphore, &amp;value);
    printf("Before wait: %d\n", value);
    pthread_mutex_lock(&amp;count_mutex);
    sem_wait(&amp;my_semaphore);
    pthread_mutex_unlock(&amp;count_mutex);
    sem_getvalue(&amp;my_semaphore, &amp;value);
    printf("Consumer semaphore value: %d\n", value);
    return 0;
}

void *producerUse(void * ptr)
{

    int pause = rand() % 5;
    sleep(pause);
    int value;
    sem_getvalue(&amp;my_semaphore, &amp;value);
    printf("Before post: %d\n", value);
    if(value &lt; 4)
    {

        sem_post(&amp;my_semaphore);
        sem_getvalue(&amp;my_semaphore, &amp;value);
        printf("Producer semaphore value: %d\n", value);
    }
    return 0;
}
General note

It is very hard to tell if this program works or not: I don't know what is supposed to be a correct behaviour. I can only speculate that you didn't want to see a semaphore value ever to exceed 4. If it is the case, than it is a pure coincidence -- caused by the large delays (try to remove sleep()). Absolutely nothing in the code prevents two or more producers to "simultaneously" test the value, and proceed to post the semaphore.

Particular problems

There is no reason to serialize consumers' access to a sem_wait(). In a sense it defeats the purpose of the semaphore.

stdio is not thread safe. The fact that printouts haven't messed up is another coincidence, also caused by the sleep() delays.

The code creates a multitude of threads, but joins only two. Not a problem in a toy program; a bad resource leak in general.

A minor note: since the thread functions ignore the argument, you should pass NULL. Passing anything else (like (void *) (intptr_t) value) is misleading.