Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is this the correct way to implement a thread-safe concurrent FIFO queue in C++? It requires passing unsigned char* arrays of binary data.

Thread Safe Concurrent Queue

#include <queue>
#include <pthread.h>

class concurrent_queue


    std::queue<unsigned char*> _queue_;
    pthread_mutex_t push_mutex;
    pthread_mutex_t pop_mutex;
    pthread_cond_t cond;


        pthread_mutex_init(&push_mutex, NULL);
        pthread_mutex_init(&pop_mutex, NULL);
        pthread_cond_init(&cond, NULL);

    void push(unsigned char* data)



    void pop(unsigned char** popped_data)

        while (_queue_.empty() == true)
            pthread_cond_wait(&cond, &pop_mutex);

        *popped_data = _queue_.front();



void *consumer_thread(void *arguments)
    concurrent_queue *cq = static_cast<concurrent_queue*>(arguments);

    while (true)
        unsigned char* data = NULL;


        if (data != NULL)
            // Eureka! Received from the other thread!!!
            // Delete it so memory keeps free.
            // NOTE: In the real scenario for which I need
            // this class, the data received are bitmap pixels
            // and at this point it would be processed

            delete[] data;

    return 0;


void main()
    concurrent_queue cq;

    // Create the consumer
    pthread_t consumer;
    pthread_create(&consumer, NULL, consumer_thread, &cq));

    // Start producing
        // Push data. 
        // Expected behaviour: memory should never run out, as the
        // consumer should receive the data and delete it.
        // NOTE: test_data in the real purpose scenario for which I 
        // need this class would hold bitmap pixels, so it's meant to 
        // hold binary data and not a string

        unsigned char* test_data = new unsigned char [8192];

    return 0;
share|improve this question
Zhenya - editing the question to incorporate suggested changes, 'invalidates' the answers that have been given. Please read the meta-post/link that ChrisW posted above, and I repeat here.... – rolfl Apr 20 '14 at 15:09
I see you're deleting the char * in consumer Thread. Isn't that a bug ? – navderm Aug 9 '15 at 19:57
The consumer is just a test. In the comment right before the delete, on the second line, is explained why is deleted. In a real scenario you will not delete it until you have used for your own purposes, but after using it you will need to delete it to avoid memory leaks. – PerracoLabs Aug 9 '15 at 20:00

You are guarding the state of one variable

std::queue<unsigned char*> _queue_;

So you only have one mutex.

pthread_mutex_t push_mutex;
pthread_mutex_t pop_mutex;

If you have two then push and pop can modify queue at the same time.

Make sure your mutex locking is exception safe by using RAII. Note all operations were you do start operation stop should be done by using RAII. This is the most important concept in C++ look it up.

Don't use pointers in C++. They do not convey ownership symantics (which means you don't know who should delete it (or when it should be deleted)). In this case use std::string it solves all the problems.

I see calls pthread_XXX_init but not the equivalent destroy.

Because you have not defined them the default copy constructor and assignment operator will be created. Since your object contains resources (mutext and conditional) you probably don't want the default behavior. Either define them or disable them.

C++11 has a better set of threading constructs use those rather than the C pthread library.

share|improve this answer
I find std::vector easier to deal with when handling binary data (assuming the unsigned char * pointer is for binary data.) – Alexis Wilke Feb 14 '14 at 4:42
@AlexisWilke: Me too. I was assuming a string. But for binary data I would use a vector. – Loki Astari Feb 14 '14 at 5:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.