Sign in to follow this  
wh1sp3rik

Windows mutex

Recommended Posts

Hello again :)

I have another question, which is unclear to me.
Do you think, i need to use mutexes, when one thread is reading only from shared variable and second is writing only to that variable ? what about, if all threads are reading only from shared variable ?

thank you :)

Share this post


Link to post
Share on other sites
No, that is not necessary and is overkill.

A full Windows mutex is an incredible bottleneck to the system. They are occasionally needed, but generally they should be avoided.

Google is full of examples that can show you just how heavyweight Windows mutex objects are. One of the 59 million Google hits showed this:

------------------------------- Core 2 Duo E6850 --------- Core 2 Quad Q9550
- mutex | 180791 ms | 17630195 ms
- spin_mutex | 78605 ms | 102566 ms
- queuing_mutex | 164113 ms | 216254 ms
- Conc.Hastable | 1422683 ms | 2066588 ms
- Parallel while + for | 495898 ms | 556969 ms
- Parallel pipe/queue | 924551 ms | 1409204 ms
- Parallel reduce | 65100 ms | 61047 ms
- Parallel scan | 116001 ms | 120051 ms
- Parallel tasks | 113971 ms | 113051 ms

Imagine a high-speed professional race track, and you install a stop sign, blocking traffic so everybody must stop, and proceed single-file and individually. That is the effect of a global mutex. You end up hurting the performance not only of your app, but can also impact the performance of other programs running on your system.



A Windows global mutex is almost always the wrong choice for synchronization primitive.




In the case of a single variable, you just need to ensure atomic access. You can tell your OS to protect a single variable by using interlocked operations.

Share this post


Link to post
Share on other sites
Quote:
Original post by wh1sp3rik
Hello again :)

I have another question, which is unclear to me.
Do you think, i need to use mutexes, when one thread is reading only from shared variable and second is writing only to that variable ? what about, if all threads are reading only from shared variable ?

thank you :)



In a scenario where a shared variable will only ever be read and never written, no synchronization is necessary whatsoever. As soon as even single writer comes into play, you definitely do need some kind of synchronization. As frob has already pointed out, what Windows likes to call a mutex is usually not the right choice for this. You can either do what he suggested and use interlocked ops, or you can use InitializeCriticalSection() / EnterCriticalSection() / LeaveCriticalSection() / DeleteCriticalSection() functions to synchronize access to the shared variable. link

If you want to do this in a platform-independent manner and using an API that is a lot safer and cleaner, I'd suggest you take a look at boost.thread, specifically its mutex types. link

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this