Boost message_queue: when I receive an object, how can I remove it from the queue? [closed]
I noticed that, when I get an object from a Boost message_queue with the *receive() member functions, that object remains in the queue. So if I read the same message_queue multiple times, I keep getting copies of the same objects, even if nothing has been inserted from the other side.
Is there a way to remove an object from the queue once it has been read?
E.g.:
boost::scoped_ptr<boost::interprocess::message_queue> pMQueue;
message_queue::remove("myQueue");
pMQueue.reset(new message_queue(open_or_create, "myQueue", 100, sizeof(MyClass)));
MyClass token;
std::vector<MyClass> tokens;
size_t recvdSize = 0;
float timeDelay = 100; // milliseconds
bool dataAvailable = true;
// If I do this block twice, I receive the same tokens twice
while(dataAvailable)
{
ptime t = microsec_clock::universal_time() + milliseconds(timeDelay);
dataAvailable = pMQueue->timed_receive(&token, sizeof(token), recvdSize, 0, t);
if(dataAvailable && recvdSize > 0)
tokens.push_back(token);
else if(recvdSize == 0)
开发者_开发百科 break;
else if(recvdSize != sizeof(token))
exit(1);
}
Ops, I found a bug in my code. The message_queue works as expected, the problem was in the way the tokens container was managed.
精彩评论