博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
windows多线程同步机制---事件
阅读量:3986 次
发布时间:2019-05-24

本文共 1842 字,大约阅读时间需要 6 分钟。

  1 事件

    通知的作用,当收到事件时,线程可以执行.
    否则,线程将等候事件发生.,事件只有0无信号和1有信号两种状态
    

  2 事件的用法

    2.1 创建事件

HANDLE CreateEvent(
       LPSECURITY_ATTRIBUTES lpEventAttributes,
                      //安全属性
       BOOL bManualReset,//重置方式
       BOOL bInitialState, //初始化状态
       LPCTSTR lpName //名称
 
);
 
返回创建好的事件句柄
 
bManualReset - 事件重置方式, TRUE
 
 手动和FALSE自动重置. 如果为FALSE,
 
 系统在等候到事件后,会自动将事件
 
 重置为无信号状态. 如果为TRUE,我们
 
 必须自己使用ResetEvent重置状态.
      bInitialState - 初始化状态, TRUE为
         有信号,FALSE无信号.

   2.2 等候事件

      WaitForSingleObject/
      WaitForMultipleObjects

    2.3 触发事件

//事件置为1有信号
      BOOL SetEvent(
      HANDLE hEvent //事件句柄
      );

    2.4 关闭事件

      CloseHandle

    2.5 重置事件

//事件置为0无信号

      BOOL ResetEvent(

       HANDLE hEvent //事件句柄
      );

    2.6 其他函数

      OpenEvent

      PulseEvent

// Event.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "conio.h"#include "windows.h"HANDLE g_hEvent = NULL;HANDLE g_hEvent2= NULL;DWORD WINAPI ThreadSend( LPVOID pParam ){	while( 1 )	{	//触发事件(置为有信号)		SetEvent( g_hEvent );		Sleep( 500 );		SetEvent( g_hEvent2 );		Sleep( 500 );	}	return 0;}DWORD WINAPI ThreadRecv( LPVOID pParam ){	while( 1 )	{	//等候事件通知,此事件等待到之后自动置为无信号		WaitForSingleObject( g_hEvent, 			INFINITE );		printf( "Hello Event: %p\n", 			g_hEvent );	}	return 0;}DWORD WINAPI ThreadRecv2( LPVOID pParam ){	while( 1 )	{	//等候事件通知		WaitForSingleObject( g_hEvent2, 			INFINITE );		printf( "Hello Event2: %p\n", 			g_hEvent2 );		ResetEvent( g_hEvent2 );//置为无信号	}	return 0;}void Create( ){	DWORD  nThreadID  = 0;	HANDLE hThread[3] = { NULL };	hThread[0] = CreateThread( NULL, 0,		ThreadSend, NULL, 0, &nThreadID );	hThread[1] = CreateThread( NULL, 0,		ThreadRecv, NULL, 0, &nThreadID );	hThread[2] = CreateThread( NULL, 0,		ThreadRecv2, NULL, 0, &nThreadID );}int main(int argc, char* argv[]){	//创建自动重置事件	g_hEvent = CreateEvent( NULL, 		FALSE, FALSE, NULL );	//创建手动重置事件	g_hEvent2 = CreateEvent( NULL, 		TRUE, FALSE, NULL );	Create( );		getch( );	//关闭事件	CloseHandle( g_hEvent );	return 0;}

转载地址:http://owxui.baihongyu.com/

你可能感兴趣的文章
[LeetCode By Python]13 Roman to Integer
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]107. Binary Tree Level Order Traversal II
查看>>
[LeetCode By Python]108. Convert Sorted Array to Binary Search Tree
查看>>
[leetCode By Python]111. Minimum Depth of Binary Tree
查看>>
[LeetCode By Python]112. Path Sum
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]119. Pascal's Triangle II
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode BY Python]155. Min Stack
查看>>
[LeetCode By Python]167. Two Sum II - Input array is sorted
查看>>
[LeetCode By Python]168. Excel Sheet Column Title
查看>>
[LeetCode BY Python]169. Majority Element
查看>>
[LeetCode By Python]171. Excel Sheet Column Number
查看>>
[LeetCode By Python]172. Factorial Trailing Zeroes
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
Mac删除文件&文件夹
查看>>