Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

std::queue empty() is thread safe?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jun - 04 Jul 2005 12:53 GMT
hi,

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()
}

instead of...

lock()
if( !queue.empty() )
{
  ....
}
unlock()

thanks in advance
Mihajlo Cvetanović - 04 Jul 2005 14:11 GMT
> just want to know if std::queue empty() is thread safe (running on 2 or more
> thread)... so i can do,
[quoted text clipped - 5 lines]
>    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.
Jun - 04 Jul 2005 14:39 GMT
>> just want to know if std::queue empty() is thread safe (running on 2 or
>> more thread)... so i can do,
[quoted text clipped - 19 lines]
> this can happen. This shouldn't stop you from making robust code that will
> work even when original assumptions are changed.

oops... yeah got your point,

my bad i realize this after posting ~_~

thanks for your reply btw!
Doug Harrison [MVP] - 04 Jul 2005 17:37 GMT
>> just want to know if std::queue empty() is thread safe (running on 2 or more
>> thread)... so i can do,
[quoted text clipped - 7 lines]
>
> empty() may be thread safe by itself (I don't know)

It isn't. Note also that for the sequence above to have any chance of
working, lock/unlock would have to operate on the same mutex as std::queue
uses internally, if it used a mutex internally, which it doesn't.

> but this code
> actually isn't. Consider the situation where queue isn't empty on "if"
[quoted text clipped - 4 lines]
> 1. lock *is* necessary, and
> 2. performance loss is so minimal that we shouldn't even consider it.

Point (2) doesn't necessarily apply.

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

Those "original assumptions" don't help. The code given above contains a
synchronization error no matter how you look at it.

Signature

Doug Harrison
Microsoft MVP - Visual C++

KevinLee - 05 Jul 2005 07:32 GMT
I think that most the containers provided by MFC are all not threadsafe,
you must synchronize your manipulations by yourself.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.