Simple thread synchronization

I need a simple "one at a time" lock in a section of code. Consider a func function that can be run from multiple threads:

 void func() { // locking/mutex statement goes here operation1(); operation2(); // corresponding unlock goes here operation3(); } 

I need to make sure that operation1 and operation2 always work together. With C #, I would use a simple lock block around these two calls. What is the equivalent of C ++ / Win32 / MFC?

Presumably some kind of Mutex ?

+6
c ++ winapi mfc
source share
5 answers

Critical sections will work (they are lighter than mutexes). InitializeCriticalSection, EnterCriticalSection, LeaveCriticalSection and DeleteCriticalSection are the functions to look up in MSDN.

 void func() { // cs previously initialized via InitializeCriticalSection EnterCriticalSection(&cs); operation1(); operation2(); LeaveCriticalSection(&cs); operation3();} } 

EDIT: Critical sections are faster than mutexes, since critical sections are primarily user-mode primitives. In the case of non-contact receipt (usually this is a usual case), there is no system call to the kernel, and the receipt takes about tens of cycles. The kernel switch is more expensive (about a hundred cycles). The only time that critical sections are called to the kernel is a lock, which includes waiting on the kernel primitive (either a mutex or an event). Acquiring a mutex always involves a call to the kernel and, therefore, is an order of magnitude slower. However, critical sections can only be used to synchronize resources in a single process. A mutex is required to synchronize multiple processes.

+19
source share

Commit Michael solution above.

Michael's solution is ideal for C applications. But when used in C ++, this style is discouraged by the possibility of exceptions. If an exception occurs in operation1 or in operational mode2, the critical section will not be correctly deleted, and all other threads will block the wait.

 // Perfect solutiuon for C applications void func() { // cs previously initialized via InitializeCriticalSection EnterCriticalSection(&cs); operation1(); operation2(); LeaveCriticalSection(&cs); operation3();} } // A better solution for C++ class Locker { public: Locker(CSType& cs): m_cs(cs) { EnterCriticalSection(&m_cs); } ~Locker() { LeaveCriticalSection(&m_cs); } private: CSType& m_cs; } void func() { // cs previously initialized via InitializeCriticalSection { Locker lock(cs); operation1(); operation2(); } operation3(); } 
+25
source share

The best method is to use a critical section, use EnterCriticalSection and LeaveCriticalSection. The only tick part is that you need to initialize the critical section first with InitializeCriticalSection. If this code is inside the class, put the initialization in the constructor and data structure CRITICAL_SECTION as a member of the class. If the code is not part of the class, you most likely need to use global or something similar to ensure that it is initialized once.

+6
source share
  • using MFC:

    • Define a synchronization object. (Mutext or Critical Section)

      1.1 If multiple threads belonging to another process are in func (), then use CMutex.

      1,2. If multiple threads of the same process enter the func () function, then use CCriticalSection.

    • CSingleLock can be used to facilitate the use of synchronization objects.

Let's say that we have defined a critical section

  CCriticalSection m_CriticalSection; void func() { // locking/mutex statement goes here CSingleLock aLock(&m_CriticalSection, **TRUE**); // TRUE indicates that Lock aquired during aLock creation. // if FALSE used then use aLock.Lock() for locking. operation1(); operation2(); // corresponding unlock goes here aLock.Unlock(); operation3(); } 

EDIT: refer to MSN's VC ++ article: Multithreading with C ++ and MFC Classes and Multithreading: how to use synchronization classes

+6
source share

You can try the following:

 void func() { // See answer by Sasha on how to create the mutex WaitForSingleObject (mutex, INFINITE); operation1(); operation2(); ReleaseMutex(mutex); operation3(); } 
+2
source share

All Articles