您的位置:首页 > 其它

利用事件对象实现线程同步

2010-01-13 14:53 330 查看
不废话,上代码

#include<windows.h>
#include<iostream>
using namespace std;

DWORD WINAPI fun1proc(LPVOID lpParameter);
DWORD WINAPI fun2proc(LPVOID lpParameter);
int index=0;
int tickets=100;
HANDLE g_hEvent;

void main()
{
HANDLE hThread1;
HANDLE hThread2;

g_hEvent=CreateEvent(NULL,FALSE,FALSE,"tickets"); //创建事件对象

//保证应用程序只有一个实例运行
if(g_hEvent)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only one instance can run!"<<endl;
int a;
scanf("%d",&a);
return ;

}
}

SetEvent(g_hEvent);

hThread1=CreateThread(NULL,0,fun1proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,fun2proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);

Sleep(4000);
CloseHandle(g_hEvent);
}

DWORD WINAPI fun1proc(LPVOID lpParameter)
{
while(1)
{
WaitForSingleObject(g_hEvent,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket"<<tickets--<<endl;
SetEvent(g_hEvent); //一个线程函数运行完毕后使事件对象处于有信号状态。
}
else
break;
}
return 0;
}

DWORD WINAPI fun2proc(LPVOID lpParameter)
{
while(1)
{
WaitForSingleObject(g_hEvent,INFINITE);
ResetEvent(g_hEvent);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket"<<tickets--<<endl;
SetEvent(g_hEvent);
}
else
break;

}
return 0;
}

小解释:

CreateEventA(

__in_opt LPSECURITY_ATTRIBUTES lpEventAttributes,

__in BOOL bManualReset,

__in BOOL bInitialState,

__in_opt LPCSTR lpName

);

#define CreateEvent CreateEventA

在程序中可以通过CreateEvent函数创建或打开一个命名的或匿名的事件对象,该函数的原型如上。该函数的四个参数下解释

Parameters

lpEventAttributes

A pointer to a SECURITY_ATTRIBUTES structure. If this parameter is NULL, the handle cannot be inherited by child processes.

The lpSecurityDescriptor member of the structure specifies a security descriptor for the new event. If lpEventAttributes is NULL, the event gets a default security descriptor. The ACLs in the default security descriptor for an event come from the primary or impersonation token of the creator.

bManualReset

If this parameter is TRUE, the function creates a manual-reset event object, which requires the use of the ResetEvent function to set the event state to nonsignaled. If this parameter is FALSE, the function creates an auto-reset event object, and system automatically resets the event state to nonsignaled after a single waiting thread has been released.

bInitialState

If this parameter is TRUE, the initial state of the event object is signaled; otherwise, it is nonsignaled.

lpName

The name of the event object. The name is limited to MAX_PATH characters. Name comparison is case sensitive.

If lpName is NULL, the event object is created without a name.

If lpName matches the name of an existing semaphore, mutex, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.

Return Value

If the function succeeds, the return value is a handle to the event object. If the named event object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: