Windows_39_thread_event Threads-Events

Source: Internet
Author: User

Windows_39_thread_event Thread-Event
  
 
  1. // windows_39_Thread_Event.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "windows.h"
  5. HANDLE g_hEvent = NULL;
  6. HANDLE g_hEvent2 = NULL;
  7. //发送线程
  8. DWORD WINAPI ThreadSend( LPVOID pParam )
  9. {
  10. while (1)
  11. {
  12. SetEvent( g_hEvent );
  13. Sleep( 500 );
  14. SetEvent( g_hEvent2 );
  15. Sleep( 500 );
  16. }
  17. return 0;
  18. }
  19. //接收线程
  20. DWORD WINAPI ThreadRecv( LPVOID pParam )
  21. {
  22. while (1)
  23. {
  24. //等候事件通知
  25. WaitForSingleObject( g_hEvent,INFINITE);
  26. printf( "Hello Event: %p\n", g_hEvent );
  27. }
  28. return 0;
  29. }
  30. DWORD WINAPI ThreadRecv2( LPVOID pParam )
  31. {
  32. while (1)
  33. {
  34. //等候事件通知
  35. WaitForSingleObject( g_hEvent2,INFINITE);
  36. printf( "Hello Event: %p\n", g_hEvent );
  37. Sleep( 500 );
  38. }
  39. return 0;
  40. }
  41. //线程创建
  42. void Create( )
  43. {
  44. DWORD nThreadID = 0;
  45. HANDLE hThread[3] = { NULL };
  46. //创建触发事件
  47. hThread[0] = CreateThread( NULL, 0, ThreadSend, NULL, 0, &nThreadID );
  48. HThread [ 1 ] Span class= "pun" >= createthread ( NULL 0 threadrecv , NULL 0 &
  49. hThread[2] = CreateThread( NULL, 0, ThreadRecv2, NULL, 0, &nThreadID );
  50. }
  51. int _tmain(int argc, _TCHAR* argv[])
  52. {
  53. //创建自动重置事件
  54. g_hEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
  55. //创建手动重置事件
  56. g_hEvent2 = CreateEvent( NULL, TRUE, FALSE, NULL );
  57. Create( );
  58. getchar( );
  59. //关闭事件
  60. CloseHandle( g_hEvent );
  61. CloseHandle( g_hEvent2 );
  62. return 0;
  63. }



From for notes (Wiz)

Windows_39_thread_event Threads-Events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.