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

    Question 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

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Understanding Synchronization in detail in MFC.?

    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Understanding Synchronization in detail in MFC.?

    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Understanding Synchronization in detail in MFC.?

    Quote Originally Posted by Igor Vartanov View Post
    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.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Understanding Synchronization in detail in MFC.?

    Quote Originally Posted by Mbatra View Post
    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).

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Understanding Synchronization in detail in MFC.?

    Quote Originally Posted by OReubens View Post
    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

  9. #9
    Join Date
    Nov 2009
    Posts
    24

    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.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Understanding Synchronization in detail in MFC.?

    Quote Originally Posted by Mbatra View Post
    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)

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Understanding Synchronization in detail in MFC.?

    Quote Originally Posted by Mbatra View Post
    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

  12. #12
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Understanding Synchronization in detail in MFC.?

    Quote Originally Posted by Igor Vartanov View Post
    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.

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Understanding Synchronization in detail in MFC.?

    Quote Originally Posted by OReubens View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured