#include <iostream>
#include <windows.h>
using namespace std;
/** thread handle */
HANDLE hServer, hClient;
/* synchronization event */
HANDLE g_hevtRequestSubmitted;
HANDLE g_hevtResultReturned;
static int tickets = 0;
char buf[30] = " 0 ";
DWORD WINAPI ServerThread( LPVOID lpParam )
{
while ( TRUE )
{
WaitForSingleObject( g_hevtRequestSubmitted, INFINITE );
ResetEvent( g_hevtRequestSubmitted ); /* manual reset */
cout << "server out: " << buf <<endl;
SetEvent( g_hevtResultReturned );
}
}
DWORD WINAPI CleientThread( LPVOID lpParam )
{
while ( TRUE )
{
ResetEvent( g_hevtResultReturned ); /* manual reset */
cout << "\n client in: ";
cin >> buf;
SetEvent( g_hevtRequestSubmitted );
WaitForSingleObject( g_hevtResultReturned, INFINITE );
}
}
int main()
{
// g_hevtRequestSubmitted = CreateEvent( NULL, FALSE, FALSE, NULL ); /* auto rest */
// g_hevtResultReturned = CreateEvent( NULL, FALSE, FALSE, NULL ); /* auto reset */
g_hevtRequestSubmitted = CreateEvent( NULL, TRUE, FALSE, NULL ); /* manual reset */
g_hevtResultReturned = CreateEvent( NULL, TRUE, FALSE, NULL ); /* manual reset */
hServer = CreateThread( NULL, 0, ServerThread, NULL, 0, NULL );
hClient = CreateThread( NULL, 0, CleientThread, NULL, 0, NULL );
while (1)
{
}
return 0;
}
Event 多线程 同步
最新推荐文章于 2021-07-07 21:39:06 发布