safe check if lockfree::queue is empty

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

safe check if lockfree::queue is empty

niXman
Hi,

I read the docs about lockfree::queue::empty() and understang what this way is not safe in multithreaded program.
The question is, how can I verify that the queue is empty?
Will it be correct the following code?

bool empty() {
   T *tmp = 0;
   if ( queue.pop(tmp) ) {
      queue.push(tmp);
      return false;
   }
   return true;
}


Thanks.

--
Regards,
niXman
___________________________________________________
Dual-target(32 & 64-bit) MinGW compilers for 32 and 64-bit Windows:
http://sourceforge.net/projects/mingwbuilds/
___________________________________________________
Another online IDE: http://liveworkspace.org/

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: safe check if lockfree::queue is empty

Steven Watanabe-4
AMDG

On 04/02/2013 03:45 AM, niXman wrote:
> Hi,
>
> I read the docs about lockfree::queue::empty() and understang what this way
> is not safe in multithreaded program.
> The question is, how can I verify that the queue is empty?

It's impossible.  The problem is that by the
time you use the answer, the state of the
queue may have changed.  You can't do better
than the built in empty.

> Will it be correct the following code?
>

No.  It will re-order the elements if there is more than one.

> bool empty() {
>    T *tmp = 0;
>    if ( queue.pop(tmp) ) {
>       queue.push(tmp);
>       return false;
>    }
>    return true;
> }
>

In Christ,
Steven Watanabe


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: safe check if lockfree::queue is empty

niXman
2013/4/2 Steven Watanabe

> It's impossible.  The problem is that by the
> time you use the answer, the state of the
> queue may have changed.  You can't do better
> than the built in empty.

Thank you, Steven.
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users