CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2007
    Posts
    90

    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.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: std::map iterator

    Quote Originally Posted by Clash View Post
    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

  3. #3
    Join Date
    May 2007
    Posts
    90

    Re: std::map iterator

    By using immutable objects you mean using the const attribute? This would make things quite hard

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: std::map iterator

    You do know what a mutex is, don't you?

  5. #5
    Join Date
    May 2007
    Posts
    90

    Re: std::map iterator

    Not really, no, and I'm not no Visual Studio if that is something from there

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: std::map iterator

    Quote Originally Posted by Clash View Post
    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

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: std::map iterator

    Quote Originally Posted by Clash View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured