Can I use std::queue as ASIO io object for asynchronise operation?

Hi, I want to implement a message queue between multiple thread, with multiple thread post message to the queue and one single thread process the message. I don't want the single processing thread to be blocked by the os when there is no message to be processed. I don't know if I can use ASIO lib to help me implement it. In my thought, the single processing thread read the queue asynchronisely and be invoked if there is one message ready on the queue, just like there is data ready on the socket. Is it doable? And is its performance better than the blocking method using conditional_variable? Regards, -Bruce

Bruce -- "liubl" <809651688@qq.com> writes:
I want to implement a message queue between multiple thread, with multiple thread post message to the queue and one single thread process the message. I don't want the single processing thread to be blocked by the os when there is no message to be processed.
I don't know if I can use ASIO lib to help me implement it. In my thought, the single processing thread read the queue asynchronisely and be invoked if there is one message ready on the queue, just like there is data ready on the socket.
Is it doable? And is its performance better than the blocking method using conditional_variable?
It's probably not exactly the same thing, and I can't speak for performance numbers, but I found the response to this question to be very enlightening: http://stackoverflow.com/questions/7754695/boost-asio-async-write-how-to-not... The basic idea is to only access a particular resource from within a single strand. I'm successfully using this pattern on one of my projects. Depending on how flexible you need your message queues to be, you might also take a look at "Zero MQ": http://www.zeromq.org/ Happy hacking, t.
participants (2)
-
Anthony Foiani
-
liubl