lock-free icon indicating copy to clipboard operation
lock-free copied to clipboard

queue empy / full check shouldn't be coded as pre-condition; instead it should return bool (success / fail)

Open mw66 opened this issue 5 years ago • 0 comments

https://github.com/MartinNowak/lock-free/blob/master/src/lock_free/rwqueue.d#L29

    void push(shared(T) t)
    in { assert(!full); }
    body ...

https://github.com/MartinNowak/lock-free/blob/master/src/lock_free/rwqueue.d#L38

    shared(T) pop()
    in { assert(!empty); }
    body ...

They are public methods, the caller may call them at some unknown state, so it's the container's responsibility to behave correctly under any condition. The best way is return a bool indicate failure or success, instead of throw assertion exception (and only in debug mode).

The reference implementation in C++ used this returning bool policy too:

https://bitbucket.org/KjellKod/lock-free-wait-free-circularfifo/src/1dcdaa20158c69f280c64dc0a0f9b11f45983a29/src/circularfifo_memory_sequential_consistent.hpp#lines-65

https://bitbucket.org/KjellKod/lock-free-wait-free-circularfifo/src/1dcdaa20158c69f280c64dc0a0f9b11f45983a29/src/circularfifo_memory_sequential_consistent.hpp#lines-75

mw66 avatar May 15 '20 23:05 mw66