C ++ multi-thread example (event synchronization object, variable sharing)

Source: Internet
Author: User
  1. // Event synchronization object
  2. # Include <windows. h>
  3. # Include <iostream. h>
  4. DWORD winapi fun1proc (lpvoid lpparameter); // thread data
  5. DWORD winapi fun2proc (lpvoid lpparameter); // thread data
  6. Int tickets = 100; // global variable
  7. Handle g_hevent; // handle
  8. Void main ()
  9. {
  10. Handle hthread1;
  11. Handle hthread2;
  12. // Create a synchronization object
  13. G_hevent = createevent (null, false, false, "tickets ");
  14. If (g_hevent) // determine whether the creation is successful
  15. {
  16. If (error_already_exists = getlasterror ())
  17. {
  18. Cout <"only one instance can run! "<Endl;
  19. Return;
  20. }
  21. }
  22. Setevent (g_hevent); // This setting is important.
  23. // Create a thread
  24. Hthread1 = createthread (null, 0, fun1proc, null, 0, null );
  25. Hthread2 = createthread (null, 0, fun2proc, null, 0, null );
  26. Closehandle (hthread1 );
  27. Closehandle (hthread2 );
  28. // Sleep the main thread for 4 seconds
  29. Sleep (4000 );
  30. // Close the event object handle
  31. Closehandle (g_hevent );
  32. }
  33. // Entry function of thread 1
  34. DWORD winapi fun1proc (lpvoid lpparameter) // thread data
  35. {
  36. While (true)
  37. {
  38. Waitforsingleobject (g_hevent, infinite );
  39. // Resetevent (g_hevent );
  40. If (tickets> 0)
  41. {
  42. Sleep (1 );
  43. Cout <"thread1 extends Ticket:" <tickets -- <Endl;
  44. Setevent (g_hevent); // This setting is important.
  45. }
  46. Else
  47. {
  48. Setevent (g_hevent); // This setting is important.
  49. Break;
  50. }
  51. }
  52. Return 0;
  53. }
  54. // Entry function of thread 2
  55. DWORD winapi fun2proc (lpvoid lpparameter) // thread data
  56. {
  57. While (true)
  58. {
  59. // Request event object
  60. Waitforsingleobject (g_hevent, infinite );
  61. // Resetevent (g_hevent );
  62. If (tickets> 0)
  63. {
  64. Sleep (1 );
  65. Cout <"thread2 extends Ticket:" <tickets -- <Endl;
  66. Setevent (g_hevent); // This setting is important.
  67. }
  68. Else
  69. {
  70. Setevent (g_hevent); // This setting is important.
  71. Break;
  72. }
  73. }
  74. Return 0;
  75. }

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.