Why is std::queue::empty() not thread-safe? Shouldn't const functions be thread-safe?
Why is the empty()
function in std::queue
not thread-safe? (See here.) Shouldn't const
functions always be thread-safe, since it's read-only?
Maybe there may be 开发者_如何学Csome mutable
variable in the class that may get written by the several threads?
Methods that don't modify the data of a class are only thread-safe if the object is never modified by any method. Otherwise a method on another thread could change the object (under a lock, correctly) and calling queue::empty()
on your thread without acquiring the lock could lead to a race condition (depending on its implementation).
精彩评论