Search code examples
c++c++11atomiclock-free

c++ atomic object lock free guarantee


I have a follow up on this question Why is is_lock_free a member function?

To sum up the point made in that question: the property of being lock-free can only be known for an instance of a type not any instance of a type.

I'd like to know if an instance is lock-free at some point during runtime, is the property guaranteed(by c++11 standard or above) to be true during the rest of the execution?

Thanks


Solution

  • The Standard doesn't guarantee it in so many words, but I'd say it's clearly implied:

    • if the instance weren't going to remain lock free, then there'd be a race condition between getting a result from is_lock_free and using the result: that's the kind of thing I'd expect explicitly mentioned in the Standard if it were a valid concern

    • 29.4/2 "The function atomic_is_lock_free(29.6) indicates whether the object is lock-free. In any given program execution, the result of the lock-free query shall be consistent for all pointers of the same type." this suggests whether the instance is lock free or not is unlikely to change during a program execution.

    If you were determined (paranoid?) to guarantee the lock-free status of instances weren't subject to change during the program run, you could simply call atomic_is_lock_free on pointers to the instances... (or one instance if you're only mildly paranoid).