开发者

Boost message_queue: when I receive an object, how can I remove it from the queue? [closed]

开发者 https://www.devze.com 2023-04-11 04:51 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

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.

0

精彩评论

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