线程同步之事件

事件主要用到4个函数:

CreateEvent() :创建一个事件对象

SetEvent():设置事件对象为有信号状态

ReSetEvent():设置事件对象为无信号状态

WaitForSingleObject():等待事件对象变成有信号状态或者是超时

CreateEvent Function

Creates or opens a named or unnamed event object.

HANDLE WINAPI CreateEvent(
  __in_opt  LPSECURITY_ATTRIBUTES lpEventAttributes,
  __in      BOOL bManualReset,
  __in      BOOL bInitialState,
  __in_opt  LPCTSTR lpName
);

Parameters

lpEventAttributes [in, optional]

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 [in]

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 [in]

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

lpName [in, optional]

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

If lpName matches the name of an existing named event object, this function requests the EVENT_ALL_ACCESS access right. In this case, the bManualReset and bInitialState parameters are ignored because they have already been set by the creating process. If the lpEventAttributes parameter is not NULL, it determines whether the handle can be inherited, but its security-descriptor member is ignored.

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

If lpName matches the name of another kind of object in the same name space (such as 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.

The name can have a "Global\" or "Local\" prefix to explicitly create the object in the global or session name space. The remainder of the name can contain any character except the backslash character (\). For more information, see Kernel Object Namespaces. Fast user switching is implemented using Terminal Services sessions. Kernel object names must follow the guidelines outlined for Terminal Services so that applications can support multiple users.

Windows 2000:   If Terminal Services is not running, the "Global\" and "Local\" prefixes are ignored. The remainder of the name can contain any character except the backslash character.

The object can be created in a private namespace. For more information, see Object Namespaces.

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.

SetEvent Function

Sets the specified event object to the signaled state.

BOOL WINAPI SetEvent(
  __in  HANDLE hEvent
);

Parameters

hEvent [in]

A handle to the event object. The CreateEvent or OpenEvent function returns this handle.

The handle must have the EVENT_MODIFY_STATE access right. For more information, see Synchronization Object Security and Access Rights.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

ResetEvent Function

Sets the specified event object to the nonsignaled state.

BOOL WINAPI ResetEvent(
  __in  HANDLE hEvent
);

Parameters

hEvent [in]

A handle to the event object. The CreateEvent or OpenEvent function returns this handle.

The handle must have the EVENT_MODIFY_STATE access right. For more information, see Synchronization Object Security and Access Rights.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

WaitForSingleObject Function

Waits until the specified object is in the signaled state or the time-out interval elapses.

To enter an alertable wait state, use the WaitForSingleObjectEx function. To wait for multiple objects, use the WaitForMultipleObjects.

DWORD WINAPI WaitForSingleObject(
  __in  HANDLE hHandle,
  __in  DWORD dwMilliseconds
);

Parameters

hHandle [in]

A handle to the object. For a list of the object types whose handles can be specified, see the following Remarks section.

If this handle is closed while the wait is still pending, the function's behavior is undefined.

The handle must have the SYNCHRONIZE access right. For more information, see Standard Access Rights.

dwMilliseconds [in]

The time-out interval, in milliseconds. If a nonzero value is specified, the function waits until the object is signaled or the interval elapses. If dwMilliseconds is zero, the function does not enter a wait state if the object is not signaled; it always returns immediately. If dwMilliseconds is INFINITE, the function will return only when the object is signaled.

Return Value

If the function succeeds, the return value indicates the event that caused the function to return. It can be one of the following values.

Return code/valueDescription
WAIT_ABANDONED 0x00000080L

The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread and the mutex state is set to nonsignaled.

If the mutex was protecting persistent state information, you should check it for consistency.

WAIT_OBJECT_0 0x00000000L

The state of the specified object is signaled.

WAIT_TIMEOUT 0x00000102L

The time-out interval elapsed, and the object's state is nonsignaled.

WAIT_FAILED (DWORD)0xFFFFFFFF

The function has failed. To get extended error information, call GetLastError.

 

这4个函数针对手动重置信号量就够了  多个信号用多信号等待函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值