F
frankw
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
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