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 some mutable variable in the class that may get written by the several threads?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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).