|
-
December 7th, 2008, 05:44 PM
#1
std::map iterator
Hello!
I have a multithreaded application that every frame iterates through a map. The problem is, one of the threads might remove one element of the map and the other one could be iterating through the map, this is bad, correct? I could instead of directly removing, just mark the element for elimination and before the loop that iterates through the elements remove those elements. Is this the recommended way, is there another way or am I just doing everything wrong?
If I haven't gave enough information please tell me. Thanks in advance guys.
-
December 7th, 2008, 05:51 PM
#2
Re: std::map iterator
 Originally Posted by Clash
Hello!
I have a multithreaded application that every frame iterates through a map. The problem is, one of the threads might remove one element of the map and the other one could be iterating through the map, this is bad, correct? I could instead of directly removing, just mark the element for elimination and before the loop that iterates through the elements remove those elements. Is this the recommended way, is there another way or am I just doing everything wrong?
If I haven't gave enough information please tell me. Thanks in advance guys.
1) STL is NOT thread safe, YOU must handle this.
2) Modifying a collection may invalidate iterators.
Those two facts should answer your questions.
One solution is to use immutable objects when dealing with multi-threaded applications. This eliminates the need to EVER perform synchronization. The technique does take a boit of getting used to....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 7th, 2008, 06:14 PM
#3
Re: std::map iterator
By using immutable objects you mean using the const attribute? This would make things quite hard
-
December 7th, 2008, 06:17 PM
#4
Re: std::map iterator
You do know what a mutex is, don't you?
-
December 7th, 2008, 06:24 PM
#5
Re: std::map iterator
Not really, no, and I'm not no Visual Studio if that is something from there
-
December 7th, 2008, 06:40 PM
#6
Re: std::map iterator
 Originally Posted by Clash
By using immutable objects you mean using the const attribute? This would make things quite hard
"Conventional" synchronization (Mutex, Semaphore, Lock, etc) all impose a performance penalty on EVERY access, even when they are there to protect against a "one in a million" chance. Also it is virtually impossible to make sure that your object view is consistant without talking looooong locks.
using imutable (yes that means constant) objects removes this completely, and is the methodology of choice as parallism increases (what happens when your code is running of a 32+ core processor???).
As a psuedo example:
Code:
template<T>
class Collection
{
public:
Collection<T> Add(T item) const;
{
Collection<T> retVal(*this);
retVal.InternalAdd(item);
return item;
}
Collection<T> lRemove(T item)
{
Collection<T> retVaxl(*this);
retVal.InternalRemove(item);
return item;
}
private:
Collection<T> InternalAdd(T item)
{
// actuall add the object to this..
}
Collection<T> InternalRemove(T item)
{
// actuall add the object to this..
}
}
Now once a collection is created, it can never be modified. To fill from a list (this would be optimized in real code)
Code:
Collection<T> col = new Collection<T>();
foreach (Item item in source)
col = col.Add(item);
return col;
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 8th, 2008, 06:05 PM
#7
Re: std::map iterator
 Originally Posted by Clash
Not really, no, and I'm not no Visual Studio if that is something from there
Learn. You have to understand mutexes *at the least* before you do anything else with multi-threading. Personally I like pthreads or Boost::thread, since it's cross-platform, but the next revision of the C++ standard will contain standard threading mechanisms including mutexes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|