-
July 16th, 2013, 12:50 AM
#1
Understanding Synchronization in detail in MFC.?
Hi,
I want to understand synchronization objects in MFC in detail.
if anybody know about any good book / tutorial, please send me the link for the same.
I want to understand Mutex / Semaphores / Events / Critical Section in detail....How to use implement and use in practical examples.
Regards,
Mbatra
-
July 16th, 2013, 04:27 AM
#2
Re: Understanding Synchronization in detail in MFC.?
-
July 16th, 2013, 04:54 AM
#3
Re: Understanding Synchronization in detail in MFC.?
Victor Nijegorodov
-
July 16th, 2013, 05:19 AM
#4
Re: Understanding Synchronization in detail in MFC.?
Not MFC related, but covers synchronisation using c++ and WIN32 by the celebrated author Jeffrey Richter.
http://www.amazon.co.uk/Windows-Via-...3970076&sr=1-1
Last edited by 2kaud; July 16th, 2013 at 05:21 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 16th, 2013, 06:24 AM
#5
Re: Understanding Synchronization in detail in MFC.?
I never used MFC sync objects though I did a lot of multithreading in MFC. The pure Win32 API way always seemed concise enough to let me get rid of additional class dependencies.
Best regards,
Igor
-
July 16th, 2013, 06:52 AM
#6
Re: Understanding Synchronization in detail in MFC.?
Originally Posted by Igor Vartanov
I never used MFC sync objects though I did a lot of multithreading in MFC. The pure Win32 API way always seemed concise enough to let me get rid of additional class dependencies.
The point about the MFC objects is to allow RAII approach to synchronsiation.
initiate the sync by creating an object, and release the sync upon destruction.
Without something like this in place, your code won't be exception safe... As in... thrown exceptions may cause some sync objects to remain locked even though the portion of code they were protecting has gone out of scope. this could lead to hard to reproduce/solve bugs.
You don't have to use the MFC objects ofc, but wrapping your synchronisation in classes is a good idea.
-
July 16th, 2013, 02:37 PM
#7
Re: Understanding Synchronization in detail in MFC.?
Originally Posted by OReubens
The point about the MFC objects is to allow RAII approach to synchronsiation.
initiate the sync by creating an object, and release the sync upon destruction.
...
You don't have to use the MFC objects ofc, but wrapping your synchronisation in classes is a good idea.
And I never said I did something opposite to RAII. Operating with pure WinAPI primitives just makes me be less nervous.
Last edited by Igor Vartanov; July 18th, 2013 at 02:13 AM.
Best regards,
Igor
-
July 17th, 2013, 12:42 AM
#8
Re: Understanding Synchronization in detail in MFC.?
Hi,
Is "C++ concurrency in Action" by "ANTHONY WILLIAMS" a good book to learn about practical multithreading and synchronization.?
Regards,
Mbatra
Last edited by Mbatra; July 17th, 2013 at 12:56 AM.
-
July 17th, 2013, 05:53 AM
#9
Re: Understanding Synchronization in detail in MFC.?
Originally Posted by Mbatra
Hi,
Is "C++ concurrency in Action" by "ANTHONY WILLIAMS" a good book to learn about practical multithreading and synchronization.?
This book only covers concurrency as part of the new c++11 standard. It doesn't cover MFC or Windows specific concurrency.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 16th, 2013, 07:29 AM
#10
Re: Understanding Synchronization in detail in MFC.?
Originally Posted by Mbatra
I want to understand synchronization objects in MFC in detail.
if anybody know about any good book / tutorial, please send me the link for the same.
I want to understand Mutex / Semaphores / Events / Critical Section in detail....How to use implement and use in practical examples.
Explaining what each of these does is simple enough and there are tons of explanations out there.
There is no way to give practical examples other than very specific examples which you shouldn't just copy/paste into some bit of code of yourself and expect this to work properly. It takes a lot of practice and testing to get this right.
Fact remains that using synchronisation properly is extremely difficult.
Most devs tend to adopt a wide approach to things and use broad locking. This works, but it means you aren't getting the best performance or throughput.
But it's better than the other way around where not having enough means your program will fail somewhere down the line (usually with your most important client at the worst possible moment in time, and you can't possibly reproduce this on your own test machines).
-
July 18th, 2013, 02:29 AM
#11
Re: Understanding Synchronization in detail in MFC.?
Originally Posted by Mbatra
I want to understand synchronization objects in MFC in detail.
if anybody know about any good book / tutorial, please send me the link for the same.
In fact, there's no such a thing as "synchronization objects in MFC". There are only Windows synchronization kernel objects wrapped in a few thin MFC classes. So, to understand Windows synchronization in detail I would recommend to forget about MFC for some time and go with Richter's book already mentioned here.
BTW, there's some flaw in your plan. Not only critical section, mutex, semaphore and event can be used for synchronization purpose, but any kernel object able to signal to synchronization API can do for you.
Last edited by Igor Vartanov; July 18th, 2013 at 02:32 AM.
Best regards,
Igor
-
July 18th, 2013, 08:08 AM
#12
Re: Understanding Synchronization in detail in MFC.?
Originally Posted by Igor Vartanov
BTW, there's some flaw in your plan. Not only critical section, mutex, semaphore and event can be used for synchronization purpose, but any kernel object able to signal to synchronization API can do for you.
that "but any object able to signal" is wrong or at least incomplete.
A critical section does not signal. In fact it can't even be part of a WaitForSingleObject()/WaitForMultipleObjects() or similar type call. But it can be used for synchronisation.
That's why even though critical sections and unnamed mutexes seem to "do" exactly the same thing, they aren't interchangable.
a critical section is a lightweight lock that offers basic locking (but without signalling) wheras an unnamed mutex offers basic locking WITH signalling.
Not all OSes/platforms offer this lightweight variant and instead expect you to use an unnamed mutex anyway. And there's no problem here, other than that the needs to signal make a mutex a bit more complex and slower performing.
Other than that, synchronisation can also be achieved by means other than kernel objects. The Interlocked...() family of functions may be all you need to guarantee synchronisation across threads. And you can even make more complex synchronsiation primitives based on these. You can even make your own signalling primitives based on these, but the crucial difference is that the signalling code will be user mode instead of kernel mode and this will have consequences somewhere (which can be either good or bad depending on the needs of the program).
The "weirdest" of the non-signalling primitives are the so called "lock free" algorithms.
And other than that, there are ways to guarantee synchronisation without even using any of the above, although it'll typically result in a significant difference in performance (lock/unlock speed)compared to the kernel objects, but they'll have advantages of their own that the kernel objects can't provide.
-
July 18th, 2013, 10:20 AM
#13
Re: Understanding Synchronization in detail in MFC.?
Originally Posted by OReubens
that "but any object able to signal" is wrong or at least incomplete.
A critical section does not signal. In fact it can't even be part of a WaitForSingleObject()/WaitForMultipleObjects() or similar type call.
Synchronization API is not limited to WaitForXxxObjects only. And yes, critical section does signal to RtlpWaitForCriticalSection which EnterCriticalSection internally does, and which ultimately goes down to ZwWaitForSingleObject call. It in fact partially operates on event object opaquely used by CS API.
Best regards,
Igor
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
|