开发者

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 reimple开发者_如何转开发ment the wheel, does anyone have such a queue implemented in C ?


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜