Producer-Consumer Model with OpenMP

advertisements

I am trying to implement a single-producer-multiple-consumer model using OpenMP (I know that I could as well go for boost threads which might be better suited).

Here is my code, which is fairly simple and uses a thread-aware queue type:

bool producer_finished = false;

#pragma omp parallel default( none ) shared( producer_finished, buffer, datagen )
{
    #pragma omp sections
    {
        #pragma omp section
        { // single producer

            while( datagen ) {
                DType data = datagen.next()
                buffer.push( data );
            }

            producer_finished = true;
            #pragma omp flush( producer_finished )

        } // end omp section

        #pragma omp section
        {
            #pragma omp for schedule( static, 1 )
            for ( int i = 0; i < omp_get_max_threads() - 1; ++i ) {

                while ( ! producer_finished ) {

                    #pragma omp critical( buffer )
                    {
                        DType = buffer.pop();
                    }

                    processData( data );
                    outputData( data );

                    #pragma omp flush( producer_finished )
                }
            } // end omp for
        } // end omp section

    } //end omp sections
} // end omp parallel

The problem here is that the producer starts and pushes data until the buffer is full but the consumers never start. If I remove the section around the "for pragma" the same happens. Can you see whats wrong with my approach?

I also get this warning during compilation:

warning: work-sharing region may not be closely nested inside of work-sharing, critical, ordered or master region

It refers to the nesting of the for loop in the section. What would be the correct way to do it in this case?

Thanks for your feedback.

Edit: Just found this related question, set_omp_nested(1) does not help me. I will try putting it in separate functions...


OpenMP is designed for parallel computation, it's not a general purpose threading library. So trying to do a producer/consumer loop with OpenMP is just using the wrong tool, IMHO.