Why is std :: queue :: empty () not thread safe? Should const functions be thread safe?

Why is function empty()in std::queuenot thread safe? (See here .) Should constfunctions not always be thread safe, as they are read-only?

Maybe there can be some kind of variable in the class mutablethat can be written by several streams?

+5
source share
1 answer

Methods that do not modify class data are only thread-safe if the object is never modified by any method. Otherwise, the method of another thread could change the object (under a lock, correctly), and a call queue::empty()in your thread without acquiring a lock can lead to a race condition (depending on its implementation).

+5
source

All Articles