-
August 16th, 2010, 05:01 AM
#1
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
-
August 16th, 2010, 09:14 AM
#2
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
-
August 16th, 2010, 11:49 PM
#3
Re: Writing in one, reading from another
What the hell is the fast-path of a criticalsection?
-
August 17th, 2010, 12:26 AM
#4
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
-
August 17th, 2010, 12:43 AM
#5
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
-
August 17th, 2010, 01:43 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|