473,748 Members | 8,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::queue empty() is thread safe?

Jun
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
Nov 17 '05 #1
8 4096
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
"unnecessar y" 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.
Nov 17 '05 #2
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
"unnecessar y" 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.
Nov 17 '05 #3
Jun

"Mihajlo Cvetanović" <ma*@RnEeMtOsVe Et.co.yu> wrote in message
news:uS******** ******@TK2MSFTN GP09.phx.gbl...
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 "unnecessar y"
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.


oops... yeah got your point,

my bad i realize this after posting ~_~

thanks for your reply btw!
Nov 17 '05 #4
Jun

"Mihajlo Cvetanović" <ma*@RnEeMtOsVe Et.co.yu> wrote in message
news:uS******** ******@TK2MSFTN GP09.phx.gbl...
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 "unnecessar y"
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.


oops... yeah got your point,

my bad i realize this after posting ~_~

thanks for your reply btw!
Nov 17 '05 #5
On Mon, 04 Jul 2005 15:11:29 +0200, Mihajlo Cvetanović wrote:
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)


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"
but gets empty on "lock". To avoid this situation you must lock() before
querying the queue. If you are worrying about performance of
"unnecessar y" 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.
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.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #6
On Mon, 04 Jul 2005 15:11:29 +0200, Mihajlo Cvetanović wrote:
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)


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"
but gets empty on "lock". To avoid this situation you must lock() before
querying the queue. If you are worrying about performance of
"unnecessar y" 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.
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.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #7
I think that most the containers provided by MFC are all not threadsafe,
you must synchronize your manipulations by yourself.
Nov 17 '05 #8
I think that most the containers provided by MFC are all not threadsafe,
you must synchronize your manipulations by yourself.
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
17793
by: Philip V Pham | last post by:
These questions apply to std vector, map, and cout: I am uncertain of the thread safety for reading/writing for std templates. I know if all threads are reading concurrently, it is thread safe. However, I have this situation: Case 1: map thread 1 ---------
5
2686
by: Gernot Frisch | last post by:
Hi, can I tell e.g. a queue that it should re-allocate 512 elements each time it comes to it's boundaries? -- -Gernot int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
3
8420
by: Andy | last post by:
Hi, Is std::queue thread-safe, or shall I use some synchronization facilities such as pthread_mutex to make sure my usage is thread-safe? Thanks! - Andy
0
299
by: Jun | last post by:
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()
2
5306
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find after I've run through my unit test cases. Test Case:
7
3605
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent tho network to a client so that errors and debug messages can be displayed simultaneously anywhere. I tried to use a std::stringstream to do the job. I created the stringstream in main() and pass it as pointer into a few threads. Those threads...
0
2738
by: ecestd | last post by:
I did implement the copy constructor but still have a problem with it. It is not working. What could be wrong? #include "QueueP.h" #include <cassert // for assert #include <new // for bad_alloc #include <iostream> //typedef std::queue<QueueItemTypeQueue; using namespace std; //private:{Queue::Queue(const Queue& Q)}
29
9130
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: --Thread 1-- 1. Reads TCPIP Buffer 2. Adds Buffer to Queue (q.size() to check queue isn't full, and q.push_back(...)) 3. Signals Reading Thread Event & Goes Back to Wait for More Messages on TCPIP
19
3396
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources out there on the Internet on how to design *thread-safe* *efficient* data- structures? /Nordlöw
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.