473,575 Members | 3,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about iterating hash_map

Hi,

I have a hash_map with string as key and an object pointer as value.
the object is like

class{
public:
float a;
float b;
...
}

I have two threads, one thread is keep updating the hash_map, add new
or grab the object and update the value. Another thread is retrieving
the object and read the value randomly.

It is ok if the reading thread get the stale value sometimes, but not
acceptable if reading corrupted value inside the object.

For performance concern, I do not want to lock the hash_map when two
threads are doing as

add
find by key and grab the object and update the value inside the object

I will put a mutex inside the object to make sure the read will not
get corrupted value. However, I am little concerned when one thread
doing the find on the key while another thread is adding a new item,
which will change the size of the hash_map.

In C#, if I do not lock a collection when doing iteration, other
action on the hash_map at the same time (as an add) may cause the
exception. I have a little concerned whether the same problem could
happen on C++ hash_map. I am using Redhat with gcc, on ext/hash_map.

Thanks

Jul 31 '08 #1
5 3363
On 2008-07-31 15:50, fr****@gmail.co m wrote:
Hi,

I have a hash_map with string as key and an object pointer as value.
the object is like

class{
public:
float a;
float b;
...
}

I have two threads, one thread is keep updating the hash_map, add new
or grab the object and update the value. Another thread is retrieving
the object and read the value randomly.

It is ok if the reading thread get the stale value sometimes, but not
acceptable if reading corrupted value inside the object.

For performance concern, I do not want to lock the hash_map when two
threads are doing as

add
find by key and grab the object and update the value inside the object

I will put a mutex inside the object to make sure the read will not
get corrupted value. However, I am little concerned when one thread
doing the find on the key while another thread is adding a new item,
which will change the size of the hash_map.

In C#, if I do not lock a collection when doing iteration, other
action on the hash_map at the same time (as an add) may cause the
exception. I have a little concerned whether the same problem could
happen on C++ hash_map. I am using Redhat with gcc, on ext/hash_map.
Please note that hash_map is not part of the C++ language and as such it
is not topical in this group. What you should do is to read the
documentation for you implementation and see if inserting elements
invalidates iterators, for some containers it does not not. Also you
want to know if operations on the container are thread-safe or not.

If the operations are not thread-safe but insertions does not invalidate
iterators you only need a mutex around the insertion and around the find
operations, but not for the update.

--
Erik Wikström
Jul 31 '08 #2
hash_map is not part of STL, but it is implemented in extension. What
I am asking is about ext\hash_map under gcc implementation.
Unfortunately, the header file does not give me any answer to this.

Does anyone happen to know this?

Thanks

On Jul 31, 8:58*am, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-07-31 15:50, fra...@gmail.co m wrote:
Hi,
I have a hash_map with string as key and an object pointer as value.
the object is like
class{
public:
* float a;
* float b;
* ...
}
I have two threads, one thread is keep updating the hash_map, add new
or grab the object and update the value. Another thread is retrieving
the object and read the value randomly.
It is ok if the reading thread get the stale value sometimes, but not
acceptable if reading corrupted value inside the object.
For performance concern, I do not want to lock the hash_map when two
threads are doing as
add
find by key and grab the object and update the value inside the object
I will put a mutex inside the object to make sure the read will not
get corrupted value. However, I am little concerned when one thread
doing the find on the key while another thread is adding a new item,
which will change the size of the hash_map.
In C#, if I do not lock a collection when doing iteration, other
action on the hash_map at the same time (as an add) may cause the
exception. I have a little concerned whether the same problem could
happen on C++ hash_map. I am using Redhat with gcc, on ext/hash_map.

Please note that hash_map is not part of the C++ language and as such it
is not topical in this group. What you should do is to read the
documentation for you implementation and see if inserting elements
invalidates iterators, for some containers it does not not. Also you
want to know if operations on the container are thread-safe or not.

If the operations are not thread-safe but insertions does not invalidate
iterators you only need a mutex around the insertion and around the find
operations, but not for the update.

--
Erik Wikström
Jul 31 '08 #3
On Jul 31, 3:58 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-07-31 15:50, fra...@gmail.co m wrote:
I have a hash_map with string as key and an object pointer
as value. the object is like
class{
public:
float a;
float b;
...
}
I have two threads, one thread is keep updating the
hash_map, add new or grab the object and update the value.
Another thread is retrieving the object and read the value
randomly.
It is ok if the reading thread get the stale value
sometimes, but not acceptable if reading corrupted value
inside the object.
For performance concern, I do not want to lock the hash_map
when two threads are doing as
add
find by key and grab the object and update the value inside the object
I will put a mutex inside the object to make sure the read
will not get corrupted value. However, I am little concerned
when one thread doing the find on the key while another
thread is adding a new item, which will change the size of
the hash_map.
In C#, if I do not lock a collection when doing iteration,
other action on the hash_map at the same time (as an add)
may cause the exception. I have a little concerned whether
the same problem could happen on C++ hash_map. I am using
Redhat with gcc, on ext/hash_map.
Please note that hash_map is not part of the C++ language and
as such it is not topical in this group.
It will be in the next version of the standard (under the name
unsorted_map), and is probably available pretty much everywhere
today, with slight differences.

Threading too will be in the next version of the standard.
What you should do is to read the documentation for you
implementation and see if inserting elements invalidates
iterators, for some containers it does not not. Also you want
to know if operations on the container are thread-safe or not.
If the operations are not thread-safe but insertions does not
invalidate iterators you only need a mutex around the
insertion and around the find operations, but not for the
update.
It's a node based container, so insertions shouldn't invalidate
iterators. On the other hand, I rather suspect that any thread
safety guarantees will be given at the level of the container,
and not at the level of any particular iterator into it. So he
probably needs to lock all access, regardless of how it occurs.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 31 '08 #4
In article <679ff0c8-6fea-485d-8058-61de1adf2ad8
@m73g2000hsh.go oglegroups.com> , ja*********@gma il.com says...

[ ... ]
It will be in the next version of the standard (under the name
unsorted_map), and is probably available pretty much everywhere
today, with slight differences.
Minor typo -- you undoubtedly meant "unordered_map" . It's also currently
in TR1.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 31 '08 #5
tni
The STL containers, including the hash map are not thread safe (against
concurrent read/write access). The following applies to most STL
implementations , including the GCC one:

http://www.sgi.com/tech/stl/thread_safety.html

(It would have been a really bad decision for the standard to impose
thread safety on the STL.)
Jul 31 '08 #6

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

Similar topics

3
2800
by: Jim Lynch | last post by:
I've searched google for help with this one, but I haven't found it yet. What I want is a hash map this looks like: __gnu_cxx::hash_map<string, ArcNode> ArcNodeMap; ArcNode is a class. It looks something like this: class ArcNode { private: // communication objects
5
8613
by: peter_k | last post by:
Hi I've defined hash_map in my code using this: ------------------------------------------- #include <string> #include <hash_map.h> & namespace __gnu_cxx {
4
1723
by: filox | last post by:
so, i'm using the hash_map that comes with MS visual studio, and it seems to spend an awful lot of memory. what i basiclly have is something like this: hash_map<char*, inta; char* b = new char; int i; //some code that initializes b and i a = i; i do that for a huge amount of data and end up with 470 MB of memory for the table. now, if i...
3
3833
by: kony | last post by:
Hi there, I would much appreciate your help with the following problem. Below is the code that uses a hash_map. I want to release all the memory occupied by the hash_map for other use. Apparently clear() function is not working and the trick with swap() is half working. Does anybody know how to deallocate the hash_map? Thanks in advance. ...
1
9382
by: jayesah | last post by:
Hi All, I am developing my code with Apache stdcxx. I am bound to use STL of Apache only. Now today I need hash_map in code but as I learned, it is not available in Apache since it is not standard c++. Though it is available with GNU STL. The code module where I use hash_map will generate separate object file during compilation. This...
2
4273
by: Amit Bhatia | last post by:
Hi, I am trying to use hash maps from STL on gcc 3.3 as follows: #ifndef NODE_H #define NODE_H #include <ext/hash_map> #include "node_hasher.h" class Node; typedef hash_map<pair<int,int>, Node, Node_HasherLoc_Tree;
1
1554
by: Stefan Istrate | last post by:
On http://www.sgi.com/tech/stl/hash_map.html it says that hash_map is only an extension of the SGI implementation, not a part of the C++ standard. But in g++ 4.1.3 I can manage it to work using the header <ext/hash_mapand the namespace __gnu_cxx. So, it is present in more implementations, not just SGI. Is this a thing I can trust in? Will I...
4
3394
by: James Kanze | last post by:
On Jul 16, 10:53 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote: It depends. You might like to have a look at my "Hashing.hh" header (in the code at kanze.james.neuf.fr/code-en.html---the Hashing component is in the Basic section). Or for a discussion and some benchmarks, http://kanze.james.neuf.fr/code/Docs/html/Hashcode.html. (That...
0
899
by: frankw | last post by:
Hi, I have a hash_map with string as key and an object pointer as value. the object is like class{ public: }
0
7843
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...
0
7773
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...
1
7865
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...
0
8141
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...
0
6514
agi2029
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...
1
5660
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...
0
3795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2283
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
1
1378
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.