Hi
I am accessing a map from inside threads. There is a chance
that an element is inserted into the map, from inside any thread. Since
i don't know about thread safety of stl implementation i am using , i
use mutex for thread safety. Now comes the question. Please answer this
question assuming that i am not using a thread safe stl version.
I obtain an iterator to the map from inside the critical
section(protect ed by mutex). If i use the iterator ,outside the region
protected by mutexes , is there any problem like iterator becoming
invalid. There are chances that other thread may enter the critical
section and insert an element into the map. If this happens before i
use the iterator, will the iterator become invalid.
Thanks in advance.
The sampe code is as below
if (pthread_mutex_ lock(&mutexChec kDuplicate))
{
LOG1(ERROR, "ERROR IN LOCKING MUTEX - mutexCheckDupli cate");
}
sURLMapItr itr = mURLs.find(sURL->getAbsoluteURL ());
if( itr != mURLs.end())
{
if (pthread_mutex_ unlock(&mutexCh eckDuplicate))
{
LOG1(ERROR, "ERROR IN UNLOCKING MUTEX - mutexCheckDupli cate");
}
delete sURL;
//the QUESTION is about this statement
sURL = itr->second;
}
else
{
//insert into the map if it is not present
mURLs.insert(ma ke_pair(sURL->getAbsoluteURL (), sURL));
if (pthread_mutex_ unlock(&mutexCh eckDuplicate))
{
LOG1(ERROR, "ERROR IN UNLOCKING MUTEX - mutexCheckDupli cate");
}
}
Regards
Kiran. 3 2690
wolverine wrote:
Hi
I am accessing a map from inside threads. There is a chance
that an element is inserted into the map, from inside any thread. Since
i don't know about thread safety of stl implementation i am using , i
use mutex for thread safety. Now comes the question. Please answer this
question assuming that i am not using a thread safe stl version.
I obtain an iterator to the map from inside the critical
section(protect ed by mutex). If i use the iterator ,outside the region
protected by mutexes , is there any problem like iterator becoming
invalid. There are chances that other thread may enter the critical
section and insert an element into the map. If this happens before i
use the iterator, will the iterator become invalid.
Thanks in advance.
[code snipped]
A fully executed insertion into a map does not invalidate iterators. A fully
executed deletion from a map only invalidates iterators refering to the
deleted object. These are guarantees provided by the standard (well,
without the stuff about fully executed: the standard does not say anything
about threading; but you can take the provisions of the standard as
postconditions for the operations it describes).
Best
Kai-Uwe Bux
Kai-Uwe Bux wrote:
wolverine wrote:
Hi
I am accessing a map from inside threads. There is a chance
that an element is inserted into the map, from inside any thread. Since
i don't know about thread safety of stl implementation i am using , i
use mutex for thread safety. Now comes the question. Please answer this
question assuming that i am not using a thread safe stl version.
I obtain an iterator to the map from inside the critical
section(protect ed by mutex). If i use the iterator ,outside the region
protected by mutexes , is there any problem like iterator becoming
invalid. There are chances that other thread may enter the critical
section and insert an element into the map. If this happens before i
use the iterator, will the iterator become invalid.
Thanks in advance.
[code snipped]
A fully executed insertion into a map does not invalidate iterators. A fully
executed deletion from a map only invalidates iterators refering to the
deleted object. These are guarantees provided by the standard (well,
without the stuff about fully executed: the standard does not say anything
about threading; but you can take the provisions of the standard as
postconditions for the operations it describes).
Best
Kai-Uwe Bux
Hi
Thanks for clearing my doubts on the topic. As a novice in stl ,i
am finding this group very helpful and thinks there are a lot of people
in this world willing to help others.
The C++ standard as far as I know does not say anything on the STL
collection classes as multithreading - simple because they are designed
for a single threaded environment. As a good rule, when using the STL
collection classes within a multithread environment, all reads and
writes should be protected by some sort of lock.
Many implementations may make certain guarantees regarding iterator
invalidation on the same thread but some STl implementations may not
adhere strictly to the standard.
Of course it you initialize the collections a priori and only do reads
across threads then you dont need any synchronization .
wolverine wrote:
Kai-Uwe Bux wrote:
wolverine wrote:
Hi
>
I am accessing a map from inside threads. There is a chance
that an element is inserted into the map, from inside any thread. Since
i don't know about thread safety of stl implementation i am using , i
use mutex for thread safety. Now comes the question. Please answer this
question assuming that i am not using a thread safe stl version.
>
I obtain an iterator to the map from inside the critical
section(protect ed by mutex). If i use the iterator ,outside the region
protected by mutexes , is there any problem like iterator becoming
invalid. There are chances that other thread may enter the critical
section and insert an element into the map. If this happens before i
use the iterator, will the iterator become invalid.
Thanks in advance.
[code snipped]
A fully executed insertion into a map does not invalidate iterators. A fully
executed deletion from a map only invalidates iterators refering to the
deleted object. These are guarantees provided by the standard (well,
without the stuff about fully executed: the standard does not say anything
about threading; but you can take the provisions of the standard as
postconditions for the operations it describes).
Best
Kai-Uwe Bux
Hi
Thanks for clearing my doubts on the topic. As a novice in stl ,i
am finding this group very helpful and thinks there are a lot of people
in this world willing to help others.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alexander Stippler |
last post by:
Hi,
I've got trouble with some well known issue. Iterator invalidation. My
situation:
for (it=v.begin(); it!=v.end(); ++it) {
f(*it);
}
Under some circumstances, f may alter the container by removing the current
|
by: John |
last post by:
I want to store an array of pointers (void *) to
iterators inside a map. The idea is to reach the
map <key,info> pair faster if the data is in the
array. If not use a O(log) search.
But stl wont let me typecast a map iterator to a void *
and store it in an array. Can this be done. I was looking
at the code of stl_tree and it seems that the "value" =
key,info pair is stored in a node and never moved unless
deleted. So if map points...
|
by: Jean Paul Sartre |
last post by:
can anyone suggest a function that will spit out the dom path for an
object on id?
I'm having troubles targettting elements
so i thought, if i make a function that spits path (you send it id) then
it would help
html has:
<div name="nav"> <a href="yo">hey</a><p>this <h1 id="io">hey</h1></p></div>
i want to do something like
|
by: Steve Edwards |
last post by:
Hi,
After making an iterator to a map and stepping through a loop, I want to
delete any entries that satisfy a test from an external function.
MyMapType::const_iterator iter;
for(iter = m->begin(); iter != m->end(); ++iter)
{
a = iter->first;
|
by: Steve Edwards |
last post by:
Hi,
While iterating through a multimap, I need to replace elements that meet
certain conditions with a new element.
There doesm't seem to be a replace() function for multimaps, so I'm
inserting a new element, and then deleting the old.
typedef multimap<double, MyStructType, greater<double> >;MyMultimap
MyMultimap map = ...
| |
by: catphive.lists |
last post by:
Is there a way to call erase(iter) on a list without invalidating the
iterator? Can I make a copy of an iterator and then move forward the
original without moving the copy? I'm aware of the existence of
remove_if, but in the case I'm dealing with it would be much more
natural to use an iterator.
|
by: desktop |
last post by:
Why does insert only work when specifying an iterator plus the object to
be inserted:
std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();
t.insert(it,33);
If I use push_back instead I don't need to supply the iterator. But what
|
by: JurgenvonOerthel |
last post by:
I want to replace one element in a list<stringby a list<stringand
I need to obtain an iterator to the first element in the inserted
list.
In code:
void replace_element_by_list(list<string&my_list,
list<string>::iterator
&iter_to_remove,
const list<string>
&list_to_insert) {
|
by: khalid302 |
last post by:
I'm writing a multi-threaded application where one thread waits for a
certain std::set element to be deleted by another thread.
The waiting thread already has an iterator pointing at the element
that has to be deleted. After the other thread deletes this element,
the iterator will become invalid.
Is there a way to know if a certain iterator is invalid ?
|
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...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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.
| |