Anonym
Anonym

Reputation: 3118

Blocking queue for thread producer/consumers for win32/C

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 ?

Upvotes: 4

Views: 3487

Answers (2)

Len Holgate
Len Holgate

Reputation: 21626

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.

Upvotes: 6

Will
Will

Reputation: 75635

The PostThreadMessage/WaitForSingleObject is the appropriate way to do message queuing between threads on win32.

You can also use SetEvent() (from the producer) and WaitForSingleObject() (or WaitForMultipleObjects() if multiple queues) (in the consumer) to send a flag saying that a custom queue you've written has items.

The following pseudo-code describes this approach:

in producer...
   ...
   create item
   acquire_lock
   push item onto queue
   release_lock
   SetEvent(...)
   ...

in consumer...
   while(true)
      WaitForSingleObject(event)
      acquire_lock
      pop item from queue
      release_lock
      process item
      release item

Upvotes: 2

Related Questions