When should I use EnterCriticalSection?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: When should I use EnterCriticalSection?

  1. #1
    Join Date
    May 2011

    When should I use EnterCriticalSection?

    I am reviewing some codes and found that it uses EnterCriticalSection(&mutex); and LeaveCriticalSection(&mutex);

    Basically I just want to know the use of that codes. I already checked MSDN(http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx) and it doesn't give much information on why you should use it. I guess its for locking or something if you have multiple threads but I am not sure. Can you please give me some scenarios where I should use this.

    Thank you

  2. #2
    Join Date
    Nov 2000
    Voronezh, Russia

    Re: When should I use EnterCriticalSection?

    It's very simple: imagine you are trying to access to the same variables from 5 threads simultaneously, 2 threads reading, and 3 threads writing the value. Every thread needs to enter critical section before accessing the variable and leave the section shortly after using it. This is called "locking variable", without which you might get/set the value corrupted.
    Best regards,

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center