Writing in one, reading from another
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Writing in one, reading from another

  1. #1
    Join Date
    Mar 2003
    Posts
    126

    Writing in one, reading from another

    Hi all,

    what's the (realistic) worst case scenario that could happen if I read (iterate over equal_range) from a std::multimap in one thread while writing (inserting a new element) in another?

    I have an application which will only very rarely write (few dozen times in the first seconds, maybe once every few minutes thereafter), but often read (every couple milliseconds), and I really don't want to have to check a mutex every millisecond for those rare writes.

    My understanding of red-black trees (which I believe std::multimaps are) would lead me to believe that nothing horrible could happen, but then again my understanding of red-black trees is nowhere near perfect...

    Any other ideas for avoiding a mutex? Or should I just mutex it and get it over with?

    Thanks for the feedback!
    Zen

  2. #2
    Join Date
    Nov 2003
    Posts
    1,795

    Re: Writing in one, reading from another

    Get it over with by doing it right.

    The fast-path for acquiring a CRITICAL_SECTION is pretty darn fast. Since you rarely write, readers will take the fast-path most of the time.

    gg

  3. #3
    Join Date
    Jul 2009
    Posts
    153

    Re: Writing in one, reading from another

    What the hell is the fast-path of a criticalsection?

  4. #4
    Join Date
    Nov 2003
    Posts
    1,795

    Re: Writing in one, reading from another

    If there are no other threads trying to acquire the CS, then EnterCriticalSection can acquire the CS without making a kernel call. This is the "fast-path".

    Otherwise, the thread has to make a kernel call in order to "block" until it can acquire the CS. This is the "slow-path".

    InitializeCriticalSectionAndSpinCount() is a similar optimization in that it tries to avoid the kernel call.

    gg

  5. #5
    Join Date
    Jul 2009
    Posts
    153

    Re: Writing in one, reading from another

    If it doesnt need a kernel call, how does EnterCriticalSection make sure that function itself is thread safe?

    I mean you could think EnterCriticalSection needs a critical section for itself.. to prevent two threads from going into the critical section at the same time.. lol

  6. #6
    Join Date
    Nov 2003
    Posts
    1,795

    Re: Writing in one, reading from another

    This is where the synchronization primitives provide by your OS or threading library "take care of business" They simply do what needs to be done for that particular OS/architecture.

    Windows OS's run on x86, x64, IA64, MIPS, SHx, ARM, and PowerPC. What "needs to be done" can vary from one platform to the next.

    gg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center