Blocking queue for thread producer/consumers for win32/C

c, multithreading, winapi

Solution

Use an IO Completion Port (see here) as your queue; they don't need to be just related to I/O operations and are very easy to use and perform very well due to the way the kernel can be set to limit the number threads that run in your thread pool.

Basically you call `PostQueuedCompletionStatus()` to put items on the queue and `GetQueuedCompletionStatus()` to take them off. You don't need to worry about synchronisation etc.

If you need a little more help in getting it to work then you could take a look at my free high performance server framework which includes quite a lot of IOCP code, including a stand alone thread pool that isn't related in any way to I/O. Note that this is in C++ but it should give you a good idea of how the C API hangs together.

Problem

I'm trying to replace some thread communication with a custom queue, producer is currently using PostThreadMessage, consumer is using WaitForSingleObject/PeekMessage. http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html would be what I needed, but boost nor C++ is not an option. Not wanting to reimplement the wheel, does anyone have such a queue implemented in C ?

Original source