开发者

Why is std::queue::empty() not thread-safe? Shouldn't const functions be thread-safe?

开发者 https://www.devze.com 2023-01-29 01:46 出处:网络
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?

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消