CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2009
    Posts
    2

    does c++ map find function support multithreading?

    I am running a program that have multithread trying to get access the
    map via find function, while there is no write operation at the same
    time. but unfortunately, it crashes all the time. I am using Visual
    Studio to run [ so I believe the library it is using is from MS ]. So
    it due the fact that the find function does not support
    multithreading? as far as I know, STL does not support
    multithreading..

    Thanks

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: does c++ map find function support multithreading?

    STL does not support multithreading. However, are you sure there is no write at the same time? If so, then how are you accessing the map (from a DLL, for example)?

    Viggy

  3. #3
    Join Date
    May 2008
    Posts
    300

    Re: does c++ map find function support multithreading?

    It's not that STL doesn't support multithreading... It's just that it is not thread-safe...

    Add locks yourself. There's no point adding locks into STL in case that it would still be used without multithreading.

    If you find while inserting, it for sure will crash.
    Nope

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: does c++ map find function support multithreading?

    It's not that STL doesn't support multithreading... It's just that it is not thread-safe...
    Essentially means the same.

    You may want to use Intel's TBB. I am not sure if it supports parallel map, but it supports parallel vector, list etc.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

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

    Re: does c++ map find function support multithreading?

    I suggest using a read/write lock. This will allow parallel reads, but only one write at a time (and no reads when that occurs). Most threading libraries have one.

  6. #6
    Join Date
    May 2008
    Posts
    300

    Re: does c++ map find function support multithreading?

    As for a map, reading while writing could still lead to crash. Both reads and writes need to be guarded.
    Nope

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    Re: does c++ map find function support multithreading?

    Quote Originally Posted by DreamShore View Post
    As for a map, reading while writing could still lead to crash. Both reads and writes need to be guarded.
    Reading while writing anything in a multi-threaded app can cause issues. Unless the write operation is atomic.

    Viggy

  8. #8
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: does c++ map find function support multithreading?

    >>Unless the write operation is atomic.

    And how could it be atomic in STL map..?? Ofcourse when not properly synchronized...
    Dont forget to rate my post if you find it useful.

  9. #9
    Join Date
    Sep 2009
    Posts
    2

    Re: does c++ map find function support multithreading?

    I am sure that there is no write operation when find is in progress, in this case, is it multithread-safe?

  10. #10
    Join Date
    Nov 2003
    Posts
    1,902

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: does c++ map find function support multithreading?

    Quote Originally Posted by zeng.zinan View Post
    I am sure that there is no write operation when find is in progress, in this case, is it multithread-safe?
    Any number of read operations are allowed from different threads and long as no write operations are occurring at the same time. If you can ensure this, then you'll be okay.

    However, I would still use a reader/writer lock to ensure this will always be the case. Too often a piece of code starts out with performing operations in a specific sequence (ie. the writes at a point in time, then the reads) and there isn't any issue. Then someone else comes along and changes the program without understanding the rigid sequence in which things must be performed and random errors start appearing.

    Because of this, I would recommend that you always protect any resource that's shared between threads - regardless of whether at the time you are controlling the sequence of read and writes.

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