Here is what I essentially have:
I have thread A that periodically checks for messages and processes them.
Threads B and C need to send messages to A.
Th
If you are not on windows or if you are implementing something which is cross platform in C++, try using the Queue from ACE libraries.
ACE_Message_Queue *msg_queue;
As a sample from ACE library samples, You can then use For putting message to the queue:
ACE_NEW_RETURN (mb,
ACE_Message_Block (rb.size (),
ACE_Message_Block::MB_DATA,
0,
buffer),
0);
mb->msg_priority (ACE_Utils::truncate_cast (rb.size ()));
mb->wr_ptr (rb.size ());
ACE_DEBUG ((LM_DEBUG,
"enqueueing message of size %d\n",
mb->msg_priority ()));
// Enqueue in priority order.
if (msg_queue->enqueue_prio (mb) == -1)
ACE_ERROR ((LM_ERROR, "(%t) %p\n", "put_next"));
for getting from queue:
ACE_Message_Block *mb = 0;
msg_queue->dequeue_head (mb) == -1;
int length = ACE_Utils::truncate_cast (mb->length ());
if (length > 0)
ACE_OS::puts (mb->rd_ptr ());
// Free up the buffer memory and the Message_Block.
ACE_Allocator::instance ()->free (mb->rd_ptr ());
mb->release ();
The advantage is you can change the synchronization primitive very easily without having to write too much of code.