Jun wrote:
just want to know if std::queue empty() is thread safe (running on 2 or more
thread)... so i can do,
if( !queue.empty() )
{
lock()
....
unlock()
}
empty() may be thread safe by itself (I don't know), but this code
actually isn't. Consider the situation where queue isn't empty on "if"
but gets empty on "lock". To avoid this situation you must lock() before
querying the queue. If you are worrying about performance of
"unnecessary" lock then you have made two errors in judgment:
1. lock *is* necessary, and
2. performance loss is so minimal that we shouldn't even consider it.
Maybe in your case queue won't ever go from non-empty to empty between
"if" and "lock", because the code in the if branch is the only place
where this can happen. This shouldn't stop you from making robust code
that will work even when original assumptions are changed.