I understand the C++ standard does not talk about threading. My
question here is directed more towards what happens when a STL
container is used in a certain way. I'd appreciate any thoughts. I
re-iterate I don't want to probe into what C++ standard says when I
trample some data from multiple threads, I simply want to know if I
have understood this right.
I have a "std::map<somed atatype, someotherdataty pe> myMap", where stuff
gets inserted regularly. In a multi-threaded environment, there are
other parts of code that simultaneously read from this map.
I would like to know if I have understood this right. Consider a
scenario like this:
// thread 1 in one part of the application
busy_inserting_ stuff_into_myMa p
// while thread 2 in another part of the application
1. gets a reference to the myMap
2. starts iterating by performing an ordinary loop:
std::map<>::ite rator itrbegin = myMap.begin();
std::map<>::ite rator itrend = myMap.end();
std::map<>::ite rator itr;
for (itr = itrbegin; itr != itr.end(); ++itr)
{
}
Since thread 1 is regularly inserting stuff, is there a chance thread 2
finds all its iterators invalidated while its in the process of
looping? In other words what is the effect of inserting into a map
from one thread while simultaneously looping through the same map in
another thread? I don't care if thread 2 misses some info during the
looping (since the map gets updated all the time...)
thanks! 20 5836
**sigh**
I said: I re-iterate I don't want to probe into what C++ standard says when I trample some data from multiple threads, I simply want to know if I have understood this right.
and ended up asking:
In other words what is the effect of inserting into a map from one thread while simultaneously looping through the same map in another thread? I don't care if thread 2 misses some info during the looping (since the map gets updated all the time...)
Apologies. If someone still wants to clarify this, I'd appreciate it a
lot. thanks!
Dilip schrieb: Since thread 1 is regularly inserting stuff, is there a chance thread 2 finds all its iterators invalidated while its in the process of looping? In other words what is the effect of inserting into a map from one thread while simultaneously looping through the same map in another thread? I don't care if thread 2 misses some info during the looping (since the map gets updated all the time...)
<OT>
You should not write to a variable in one thread while reading the same
variable in other threads. Its not just about invalidating iterators.
You could even run into problems when writing to one single integer.
Thread A writes to variable "size", and Thread B starts reading it just
when Thread A wrote the first byte of "size" and left the other bytes
unchanged. So Thread B gets a byte-wise mixture of the old and the new
values of "size".
Use some kind of mutex when you access variables or objects from more
than one thread.
</OT>
Thomas
Thomas J. Gritzan wrote: Dilip schrieb: <OT> You should not write to a variable in one thread while reading the same variable in other threads. Its not just about invalidating iterators.
Understood. Sadly, I am in an unenviable position of retrofitting
multithreading into a heavily C++/STL dependant application and I
frequently keep running into corner cases like this :-(
Thomas J. Gritzan wrote: Dilip schrieb: <OT> You should not write to a variable in one thread while reading the same variable in other threads. Its not just about invalidating iterators.
I just looked at the docs for std::map and it looks like for
insertion/removal no iterators are invalidated. So atleast in this
particular case, aren't I safe?
Dilip schrieb: Thomas J. Gritzan wrote: Dilip schrieb: <OT> You should not write to a variable in one thread while reading the same variable in other threads. Its not just about invalidating iterators.
I just looked at the docs for std::map and it looks like for insertion/removal no iterators are invalidated. So atleast in this particular case, aren't I safe?
In a single thread? Yes.
On Tue, 13 Jun 2006 15:43:00 -0700, Dilip wrote: Thomas J. Gritzan wrote: Dilip schrieb: <OT> You should not write to a variable in one thread while reading the same variable in other threads. Its not just about invalidating iterators.
Understood. Sadly, I am in an unenviable position of retrofitting multithreading into a heavily C++/STL dependant application and I frequently keep running into corner cases like this :-(
You need to consult with the documentation for the platform(s) that your
STL is implemented on and see what threading guarantees it provides.
On Tue, 13 Jun 2006 15:51:07 -0700, Dilip wrote: Thomas J. Gritzan wrote: Dilip schrieb: <OT> You should not write to a variable in one thread while reading the same variable in other threads. Its not just about invalidating iterators.
I just looked at the docs for std::map and it looks like for insertion/removal no iterators are invalidated. So atleast in this particular case, aren't I safe?
No. You have no control as to what's happening behind the scenes. The
only promise you have is that after the insert/erase is completed, no
other iterators into that map have been invalidated by that operation.
(Note I said _other_ iterators. In the case of erase, the iterator you're
working on becomes invalid....)
Dilip wrote: Thomas J. Gritzan wrote: Dilip schrieb: <OT> You should not write to a variable in one thread while reading the same variable in other threads. Its not just about invalidating iterators.
I just looked at the docs for std::map and it looks like for insertion/removal no iterators are invalidated. So atleast in this particular case, aren't I safe?
No, because your inserter thread could be involved in a race with your
reader thread.
Inserter thread:
Insert-Start -------------------------------- Insert Finished
-------------------- IncrementIterat or ----------------------
Reader Thread
See what happens? Not good.
In article <11************ ********@p79g20 00cwp.googlegro ups.com>,
"Dilip" <rd*****@lycos. com> wrote: **sigh**
I said:
I re-iterate I don't want to probe into what C++ standard says when I trample some data from multiple threads, I simply want to know if I have understood this right.
and ended up asking:
In other words what is the effect of inserting into a map from one thread while simultaneously looping through the same map in another thread? I don't care if thread 2 misses some info during the looping (since the map gets updated all the time...)
Apologies. If someone still wants to clarify this, I'd appreciate it a lot. thanks!
I haven't prototyped this scenario, but I have experience implementing
std::map. I strongly suspect that this scenario could very rarely
introduce cases where the increment of the read thread gets redirected
into oblivion (say while the insert is rotating a sub-tree). Note that
"very rarely" is a worst case scenario. It means your testing probably
won't expose the bug. I wish I could give you a firmer answer than
that, but quite frankly that's a research project (which I lack
resources for). Retrofitting multithreading is not a pretty place to be.
-Howard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Woodster |
last post by:
I have declared the following
std::map<std::string, std::string> myMap;
to pass myMap to functions should I be declaring functions as:
void function(std::map<std::string, std::string>);
or is there a preferred/better method of doing this?
|
by: Antti Granqvist |
last post by:
Hello!
I have following object relations:
Competition 1--* Category 1--* Course
1
|
|
*
Course
|
by: Saeed Amrollahi |
last post by:
Dear All C++ Programmers
Hello
I am Saeed Amrollahi. I am a software engineer in
Tehran Sewerage Company. I try to use std::map and map::find member
function. I
use Visual Studio .NET. my program uses two MFC classes: CRect and
CPoint
which represents Rectangle and Point concepts (as usual) and a user
|
by: Erik Wikström |
last post by:
First of all, forgive me if this is the wrong place to ask this question,
if it's a stupid question (it's my second week with C++), or if this is
answered some place else (I've searched but not found anything).
Here's the problem, I have two sets of files, the name of a file contains a
number which is unique for each set but it's possible (even probable) that
two files in different sets have the same numbers. I want to store these...
|
by: Dan Trowbridge |
last post by:
Hi everyone,
In my attempt to port code from VS 6.0 to VS.NET I had some code break along
the way, mostly due to not adhereing closely to the C++ standard. This may
be another instance but I can't think of a good fix, or even why it broke.
The problem
In one of my CFormView derived classes I have a member variable of the
type...
| |
by: Avery Fong |
last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug
This program is developed under Visual Studio .NET 2003 in a Win32 Console Project
// VectorInsert.cpp : Defines the entry point for the console application
/
#include "stdafx.h
#include "VectorInsert.h
#ifdef _DEBU
|
by: kamaraj80 |
last post by:
Hi
I am using the std:: map as following.
typedef struct _SeatRowCols
{
long nSeatRow;
unsigned char ucSeatLetter;
}SeatRowCols;
typedef struct _NetData
|
by: Dilip |
last post by:
Hi Folks
I know the C++ standard doesn't talk about threads. However I am a
little bit curious as to what might or might not happen with a
particular scenario I encountered in my project. Its w.r.t to STL
containers so I didn't know where else to post. insights appreciated:
my application iterates over a stl::map to do some processing on its
elements:
|
by: mveygman |
last post by:
Hi,
I am writing code that is using std::map and having a bit of an issue
with its performance.
It appears that the std::map is significantly slower searching for an
element then a sequential search in a vector.
Has anyone run into this before?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |