当前位置:网站首页>Critical area, event, mutex, semaphore -- four methods to control multithread synchronization and mutex

Critical area, event, mutex, semaphore -- four methods to control multithread synchronization and mutex

2022-06-09 14:32:00 51CTO

      
      
// MultiThread.cpp : Defines the entry point for the console application .
//

#include "stdafx.h"

#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <process.h>
using namespace std;

CRITICAL_SECTION g_csA;
CRITICAL_SECTION g_csB;

UINT WINAPI PrintThread1( LPVOID lp)
{
while ( 1)
{
// Last look at this code segment , Realize with semaphore 10 Threads are synchronized and orderly
//if (WaitForSingleObject(g_hDemaphore, INFINITE) != WAIT_OBJECT_0)
//{
//continue;
//}
//
EnterCriticalSection( & g_csA);
for ( int i = 0; i < 3; i ++)
{
printf( "thread1\n");
Sleep( 1000);
}
printf( "\n");
LeaveCriticalSection( & g_csA);
}

return 0;
}

UINT WINAPI PrintThread2( LPVOID lp)
{
while ( 1)
{
EnterCriticalSection( & g_csA);
for ( int i = 0; i < 3; i ++)
{
printf( "thread2\n");
Sleep( 100);
}
printf( "\n");
LeaveCriticalSection( & g_csA);
}

return 0;
}
///
HANDLE g_hEvent;
UINT WINAPI PrintThread3( LPVOID lp)
{
while ( 1)
{
DWORD dwResult = WaitForSingleObject( g_hEvent, INFINITE);
if ( dwResult == WAIT_OBJECT_0)
{
for ( int i = 0; i < 3; i ++)
{
printf( "thread3\n");
Sleep( 500);
}
printf( "\n");
}
SetEvent( g_hEvent);
}

return 0;
}

UINT WINAPI PrintThread4( LPVOID lp)
{
while ( 1)
{
DWORD dwResult = WaitForSingleObject( g_hEvent, INFINITE);
if ( dwResult == WAIT_OBJECT_0)
{
for ( int i = 0; i < 3; i ++)
{
printf( "thread4\n");
Sleep( 300);
}
printf( "\n");
}
SetEvent( g_hEvent);
}

return 0;
}

///
HANDLE g_hMutex;
UINT WINAPI PrintThread5( LPVOID lp)
{
while ( 1)
{
DWORD dwResult = WaitForSingleObject( g_hMutex, 500); // The current thread owns the mutex
if ( dwResult == WAIT_OBJECT_0)
{
for ( int i = 0; i < 3; i ++)
{
printf( "thread5\n");
Sleep( 200);
}
printf( "\n");
}
if ( ! ReleaseMutex( g_hMutex)) // The current thread releases the mutex
{
DWORD dwError = GetLastError();
if ( dwError == ERROR_NOT_OWNER)
{
printf( "thread5 error:ERROR_NOT_OWNER\n");
}
else
{
printf( "thread5 error:%d\n", dwError);
}
}
}

return 0;
}

UINT WINAPI PrintThread6( LPVOID lp)
{
while ( 1)
{
DWORD dwResult = WaitForSingleObject( g_hMutex, 1000);
if ( dwResult == WAIT_OBJECT_0)
{
for ( int i = 0; i < 3; i ++)
{
printf( "thread6\n");
Sleep( 400);
}
printf( "\n");
}
if ( ! ReleaseMutex( g_hMutex)) // If mutexes are used by threads 5 occupy , Then the current thread cannot be released , Only the possessor can release
{
DWORD dwError = GetLastError();
if ( dwError == ERROR_NOT_OWNER)
{
printf( "thread6 error:ERROR_NOT_OWNER\n");
}
else
{
printf( "thread6 error:%d\n", dwError);
}
}
}

return 0;
}
//
HANDLE g_hDemaphore;
UINT WINAPI PrintThread7( LPVOID lp)
{
while ( 1)
{
DWORD dwResult = WaitForSingleObject( g_hDemaphore, INFINITE); // Occupy a resource count
if ( dwResult == WAIT_OBJECT_0)
{
printf( "Anglela");
Sleep( 1000);
}

LONG count;
ReleaseSemaphore( g_hDemaphore, 1, & count); // Release a resource count
}

return 0;
}

UINT WINAPI PrintThread8( LPVOID lp)
{
while ( 1)
{
DWORD dwResult = WaitForSingleObject( g_hDemaphore, INFINITE); // Reduce the resource count by one ( If there are resource counts )
if ( dwResult == WAIT_OBJECT_0)
{
printf( "Baby\n");
printf( "\n");
Sleep( 1000);
}

LONG count;
ReleaseSemaphore( g_hDemaphore, 1, & count); // Increase a resource count

Sleep( 2000); // Decrease does not immediately increase
}

return 0;
}

UINT WINAPI PrintThread9( LPVOID lp)
{
while ( 1)
{
DWORD dwResult = WaitForSingleObject( g_hDemaphore, INFINITE);
Sleep( 1000);
if ( dwResult == WAIT_OBJECT_0)
{
printf( "Alice ");
Sleep( 500);
}

LONG count;
BOOL bFlag = ReleaseSemaphore( g_hDemaphore, 3, & count);
if ( ! bFlag)
{
printf( "\n");
printf( "thread9 release failed:%d\n", GetLastError());
}
}

return 0;
}

UINT WINAPI PrintThread10( LPVOID lp)
{
DWORD dwResult = 0;
while ( 1)
{
// Reduce 2 Resource count
dwResult = WaitForSingleObject( g_hDemaphore, INFINITE);
dwResult = WaitForSingleObject( g_hDemaphore, INFINITE);
if ( dwResult == WAIT_OBJECT_0)
{
printf( "in Wonderland\n");
printf( "\n");
Sleep( 1000);
}

:: EnterCriticalSection( & g_csB);
LONG count; // if rcount+pcount>maxcount, Failed to increase the resource count and the current resource count remains unchanged
BOOL bFlag = ReleaseSemaphore( g_hDemaphore, 2 /*relasecount*/, & count /*previous count*/); // increase 2 Resource count
if ( ! bFlag)
{
printf( "\n");
printf( "thread10 release failed:%d\n", GetLastError());
}
printf( "thread10 previous count:%d\n", count); // perform release Previous resource count
:: LeaveCriticalSection( & g_csB);
}

return 0;
}

// Except for the critical zone , Others are kernel objects , In addition to threads, it can also be used for processes , You can open the Kwa process using the name specified during creation
//
int _tmain( int argc, _TCHAR * argv[])
{
InitializeCriticalSection( & g_csA);
InitializeCriticalSection( & g_csB);

HANDLE m_hThread = NULL;

// Synchronization and mutual exclusion :‘1 A producer -N Consumers ’ Model , Synchronous and orderly - Must be produced and then consumed , Mutually exclusive disorder , Or execute A, Or execute B,AB Order unknown
// Because of the thread 1 More than threads 2 Execute first , The critical area guarantees the printed results thread1,thread2 Orderly
//m_hThread =(HANDLE)_beginthreadex(NULL, 0, PrintThread1, NULL, 0, NULL);
//m_hThread =(HANDLE)_beginthreadex(NULL, 0, PrintThread2, NULL, 0, NULL);

// Use events to control threads 3,4 Orderly execution
// Create a named auto reset event kernel object , The second parameter FALSE Specify automatic reset , The third parameter TRUE Set initialization to signaled
g_hEvent = CreateEvent( NULL, FALSE, TRUE, LPCWSTR( "myevent"));
if ( g_hEvent)
{
if ( ERROR_ALREADY_EXISTS == GetLastError())
{
printf( "error:event alread exist\n");
}
}
// You can open event objects created by other processes
//$ g_hEvent = OpenEventA(EVENT_ALL_ACCESS, TRUE, LPCWSTR("myevent"));
//$ ResetEvent(g_hEvent);

//m_hThread =(HANDLE)_beginthreadex(NULL, 0, PrintThread3, NULL, 0, NULL);
//m_hThread =(HANDLE)_beginthreadex(NULL, 0, PrintThread4, NULL, 0, NULL);

// The second parameter TRUE,main Threads have mutexes , Reflect mutual exclusion ‘ Thread owned ’ The concept of , Distinguishing events ‘ First come first served basis ’
g_hMutex = CreateMutexA( NULL, TRUE, "mymutex");
//$ g_hMutex = OpenMutexA(MUTEX_ALL_ACCESS, TRUE, "mymutex");

/*m_hThread =(HANDLE)_beginthreadex(NULL, 0, PrintThread5, NULL, 0, NULL);
m_hThread =(HANDLE)_beginthreadex(NULL, 0, PrintThread6, NULL, 0, NULL);
//$ The event object is set to automatic reset, which has similar functions to the mutex
Sleep(3000);
printf("main thread release mutex\n");
printf("\n");
ReleaseMutex(g_hMutex);*/

// Semaphores reflect ‘ Resource count ’ The concept of , Print orderly by modifying the initial count and releasing the increase calculation
// SEMAPHORE_MODIFY_STATE, The initial resource count is 2 when , The semaphore is in the signaled state , The maximum allowed resource count is 10 when , The semaphore is in the signaled state
g_hDemaphore = CreateSemaphoreA( NULL, 2, 10,( "Sem")); // The initial resource count is 1, The maximum allowed resource count is 2, adopt release Increase resource count
//$ HANDLE semaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, _T("Global\\TestSemaphore"));
m_hThread =( HANDLE) _beginthreadex( NULL, 0, PrintThread7, NULL, 0, NULL);
m_hThread =( HANDLE) _beginthreadex( NULL, 0, PrintThread8, NULL, 0, NULL);
m_hThread =( HANDLE) _beginthreadex( NULL, 0, PrintThread9, NULL, 0, NULL);
m_hThread =( HANDLE) _beginthreadex( NULL, 0, PrintThread10, NULL, 0, NULL);

while ( getchar() != 'x')
{
}
DeleteCriticalSection( & g_csA);
DeleteCriticalSection( & g_csB);

CloseHandle( m_hThread);

CloseHandle( g_hEvent);

if ( WaitForSingleObject( g_hMutex, 0) == WAIT_ABANDONED)
{
ReleaseMutex( g_hMutex);
}
CloseHandle( g_hMutex);

return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270.
  • 271.
  • 272.
  • 273.
  • 274.
  • 275.
  • 276.
  • 277.
  • 278.
  • 279.
  • 280.
  • 281.
  • 282.
  • 283.
  • 284.
  • 285.
  • 286.
  • 287.
  • 288.
  • 289.
  • 290.
  • 291.
  • 292.
  • 293.
  • 294.
  • 295.
  • 296.
  • 297.
  • 298.
  • 299.
  • 300.
  • 301.
  • 302.
  • 303.
  • 304.
  • 305.
  • 306.
  • 307.
  • 308.
  • 309.
  • 310.
  • 311.
  • 312.
  • 313.
  • 314.
  • 315.
  • 316.
  • 317.
  • 318.
  • 319.
  • 320.
  • 321.
  • 322.
  • 323.
  • 324.

1. Threads 1,2 Achieve printing thread1,thread2 Orderly ,thread3~thread6 Realize similar printing function ;

2. Threads 7,8,9,10 Print two ordered strings , The printing order is realized by modifying the initial count and increasing or decreasing the count ;

3.MFC take 4 Method encapsulation 4 The functions of the two classes are the same , Except for the critical zone , other 3 Can be used between processes .



原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091232333443.html