Thread Synchronization and thread synchronization
Thread Synchronization:
I. mutex Synchronization
1. Atomic access is a windows-only thread synchronization
InterlockedExchange Value
InterlockedIncrement ++
InterlockedDecrement --
InterlockedExchangeAdd + =
Implement rotation lock: Lock a piece of code
Void CThreadOurDlg: OnBnClickedButton1 ()
{
AfxBeginThread (& CThreadOurDlg: ThreadA, this );
AfxBeginThread (& CThreadOurDlg: ThreadB, this );
}
Long int g_x = 0;
BOOL flag = FALSE;
Long int a = 100;
Long int B = 200;
UINT _ cdecl CThreadOurDlg: ThreadA (LPVOID lpvoid)
{
While (InterlockedExchange (LONG *) & flag, TRUE) = TRUE)
Sleep (0 );
A = a + B;
TRACE ("-------------------------------- \ n ");
TRACE ("% d \ n", );
TRACE ("-------------------------------- \ n ");
InterlockedExchange (LONG *) & flag, FALSE );
Return 0;
}
UINT _ cdecl CThreadOurDlg: ThreadB (LPVOID lpvoid)
{
While (InterlockedExchange (LONG *) & flag, TRUE) = TRUE)
Sleep (0 );
A = a + B;
TRACE ("++ \ n ");
TRACE ("% d \ n", );
TRACE ("++ \ n ");
InterlockedExchange (LONG *) & flag, FALSE );
Return 0;
}
Rotate the lock to sell tickets:
Long nTicket = 1000;
BOOL nFlag = FALSE;
UINT _ cdecl CThreadOurDlg: ThreadFun (LPVOID lpvoid)
{
Int I = (int) lpvoid;
While (1)
{
If (nTicket <= 0)
{
Break;
}
If (nTicket> 0)
{
// -------------------------------------------------------------- Yoke ----------------------------------------
While (InterlockedExchange (LONG *) & nFlag, TRUE) = TRUE)
Sleep (0 );
CString str;
Str. Format (_ T ("% d window sells % d tickets"), I, nTicket );
TRACE ("% d sell % d tickets \ n", I, nTicket );
InterlockedDecrement (& nTicket );
// ------------------------------------------------------------- Unlock ----------------------------------------------
InterlockedExchange (LONG *) & nFlag, FALSE );
}
Sleep (20 );
}
TRACE ("% d window no votes \ n", I );
Return 0;
}
Void CThreadOurDlg: OnBnClickedButton2 ()
{
// Create 10 worker threads
For (int I = 0; I <10; I ++)
{
AfxBeginThread (& CThreadOurDlg: ThreadFun, (LPVOID) I );
}
}
2. Critical Section
2.1 Windows functions
CRITICAL_SECTION cs;
: InitializeCriticalSection (& cs); initialize the critical section
: InitializeCriticalSectionAndSpinCount (& cs, 4000); contains a rotation lock
: DeleteCriticalSection (& cs); destroy
: TryEnterCriticalSection attempts to enter the critical section
: EnterCriticalSection (& (CThreadOurDlg *) AfxGetApp ()-> m_pMainWnd)-> cs); enter
: LeaveCriticalSection (& (CThreadOurDlg *) AfxGetApp ()-> m_pMainWnd)-> cs); exit
2.2 MFC Functions
CCriticalSection o_cs;
O_cs.Lock ();
O_cs.Unlock ();
3. mutex Kernel Mode System Management
3.1 MFC class
Void CThreadOurDlg: OnBnClickedButton2 ()
{
// Create 10 worker threads
For (int I = 0; I <10; I ++)
{
AfxBeginThread (& CThreadOurDlg: ThreadFun, (LPVOID) I );
}
// Release mutex
: ReleaseMutex (mx. m_hObject );
}
UINT _ cdecl CThreadOurDlg: ThreadFun (LPVOID lpvoid)
{
Int I = (int) lpvoid;
While (1)
{
If (nTicket <= 0)
{
Break;
}
// ------------------------------------ Wait for the mutex signal ------------------------------------
: WaitForSingleObject (CThreadOurDlg *) AfxGetApp ()-> m_pMainWnd)-> mx. m_hObject, INFINITE );
If (nTicket> 0)
{
CString str;
Str. Format (_ T ("% d window sells % d tickets"), I, nTicket );
TRACE ("% d sell % d tickets \ n", I, nTicket );
(CThreadOurDlg *) AfxGetApp ()-> m_pMainWnd)-> m_listBox.AddString (str );
InterlockedDecrement (& nTicket );
}
// Release mutex
: ReleaseMutex (CThreadOurDlg *) AfxGetApp ()-> m_pMainWnd)-> mx. m_hObject );
Sleep (20 );
}
TRACE ("% d window no votes \ n", I );
Return 0;
}
3.2 HANDLE m_hMutex;
// Create a mutex
M_hMutex = CreateMutex (NULL, FALSE, NULL );
CloseHandle (m_hMutex );
M_hMutex = NULL;
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.