简体   繁体   中英

What exactly is the meaning of “wait-free” in boost::lockfree?

I am reading the docs for spsc_queue and even after reading a bit elsewhere I am not completely convinced about the meaning of "wait-free".

What exactly do they mean here

bool push(T const & t);

Pushes object t to the ringbuffer.

Note: Thread-safe and wait-free

I mean there must be some overhead for synchronisation. Is

some_spscqueue.push(x);

guaranteed to take a constant amount of time? How does it compare to a non-thread safe queue?

PS: dont worry, I am going to measure, but due to my naive ignorance I just cant imagine a synchronisation mechanism that does not involve some kind of waiting and I am quite puzzled what "wait-free" is supposed to tell me.

A wait-free implementation of a concurrent data object is one that guarantees that any process can complete any operation in a finite number of steps, regardless of the execution speeds of the other processes.

(from the abstract of the Herlihy article).

See also Wikipedia , and all the other resources you immediately find by typing "wait free" into a search engine.

So wait-free doesn't mean no delay , it means you never enter a blocking wait-state, and can't be indefinitely blocked and/or starved.

In other words, wait has the specific technical meaning that your thread is either parked (and does not execute any instructions until something wakes it up), or is in a loop waiting for some external condition to be satisfied (eg. a spinlock). If it's never woken up, or if it is woken up but always finds it can't proceed and has to wait again, or if the loop never exits, then your thread is being starved and cannot make progress.

Every operation has some latency, and wait-free doesn't say anything about what that is.

How does it compare to a non-thread safe queue?

It'll almost certainly be more expensive than a wholly-unsynchronized container, because you're still doing extra work (assuming you really do only access the container from a single thread).

"Pushes object to the ring buffer" refers to the type of array buffer that the queue is implemented on. This is probably implemented using a circular array to store the objects in the queue. See for example:

https://en.wikipedia.org/wiki/Circular_buffer

and

http://opendatastructures.org/ods-python/2_3_ArrayQueue_Array_Based_.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM