编写环境:VS2005 VC++ 控件台编程
一、使用临界区对象
#include
<
windows.h
>
#include < iostream >
using namespace std;
DWORD WINAPI Fun1Proc(LPVOID lpParameter);
DWORD WINAPI Fun2Proc(LPVOID lpParameter);
int tickets = 100 ;
CRITICAL_SECTION g_csA;
CRITICAL_SECTION g_csB;
void main()
{
HANDLE hThread1;
HANDLE hThread2;
hThread1 = CreateThread( NULL , 0 ,Fun1Proc, NULL , 0 , NULL );
hThread2 = CreateThread( NULL , 0 ,Fun2Proc, NULL , 0 , NULL );
CloseHandle(hThread1);
CloseHandle(hThread2);
InitializeCriticalSection( & g_csA);
InitializeCriticalSection( & g_csB);
Sleep( 40000 );
DeleteCriticalSection( & g_csA);
DeleteCriticalSection( & g_csB);
}
DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
while ( TRUE )
{
EnterCriticalSection( & g_csA);
Sleep( 1 );
// EnterCriticalSection( & g_csB); // 临界区的同步和互锁
if (tickets > 0 )
{
Sleep( 1 );
cout << " Thread1 sell ticket : " << tickets --<< endl;
// LeaveCriticalSection( & g_csB);
LeaveCriticalSection( & g_csA);
}
else
{
// LeaveCriticalSection( & g_csB);
LeaveCriticalSection( & g_csA);
break;
}
}
return 0 ;
}
DWORD WINAPI Fun2Proc(LPVOID lpParameter)
{
while ( TRUE )
{
EnterCriticalSection( & g_csB);
Sleep( 1 );
EnterCriticalSection( & g_csA);
if (tickets > 0 )
{
Sleep( 1 );
cout << " Thread2 sell ticket : " << tickets --<< endl;
LeaveCriticalSection( & g_csA);
LeaveCriticalSection( & g_csB);
}
else
{
LeaveCriticalSection( & g_csA);
LeaveCriticalSection( & g_csB);
break;
}
}
return 0 ;
}
#include < iostream >
using namespace std;
DWORD WINAPI Fun1Proc(LPVOID lpParameter);
DWORD WINAPI Fun2Proc(LPVOID lpParameter);
int tickets = 100 ;
CRITICAL_SECTION g_csA;
CRITICAL_SECTION g_csB;
void main()
{
HANDLE hThread1;
HANDLE hThread2;
hThread1 = CreateThread( NULL , 0 ,Fun1Proc, NULL , 0 , NULL );
hThread2 = CreateThread( NULL , 0 ,Fun2Proc, NULL , 0 , NULL );
CloseHandle(hThread1);
CloseHandle(hThread2);
InitializeCriticalSection( & g_csA);
InitializeCriticalSection( & g_csB);
Sleep( 40000 );
DeleteCriticalSection( & g_csA);
DeleteCriticalSection( & g_csB);
}
DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
while ( TRUE )
{
EnterCriticalSection( & g_csA);
Sleep( 1 );
// EnterCriticalSection( & g_csB); // 临界区的同步和互锁
if (tickets > 0 )
{
Sleep( 1 );
cout << " Thread1 sell ticket : " << tickets --<< endl;
// LeaveCriticalSection( & g_csB);
LeaveCriticalSection( & g_csA);
}
else
{
// LeaveCriticalSection( & g_csB);
LeaveCriticalSection( & g_csA);
break;
}
}
return 0 ;
}
DWORD WINAPI Fun2Proc(LPVOID lpParameter)
{
while ( TRUE )
{
EnterCriticalSection( & g_csB);
Sleep( 1 );
EnterCriticalSection( & g_csA);
if (tickets > 0 )
{
Sleep( 1 );
cout << " Thread2 sell ticket : " << tickets --<< endl;
LeaveCriticalSection( & g_csA);
LeaveCriticalSection( & g_csB);
}
else
{
LeaveCriticalSection( & g_csA);
LeaveCriticalSection( & g_csB);
break;
}
}
return 0 ;
}