Windows Thread (producer and consumer issues)

Source: Internet
Author: User

Windows Thread (producer and consumer problems) reprinted by qiqiang 2008.10.10

The producer-consumer problem is a classic process synchronization problem first proposed by Dijkstra to demonstrate the semaphore mechanism proposed by him. Two threads executed in the same process address space. The producer thread produces the item and places the item in an empty buffer for consumption by the consumer thread. The consumer thread obtains the item from the buffer and then releases the buffer. When the producer thread produces an item, if no empty buffer zone is available, the producer thread must wait for the consumer thread to release an empty buffer zone. When the consumer thread consumes an item, if the buffer is not full, the consumer thread will be blocked until the new item is produced.

  1. # Include <windows. h>
  2. # Include <iostream>
  3. Const unsigned short size_of_buffer = 10; // buffer Length
  4. Unsigned short productid = 0; // Product NO.
  5. Unsigned short consumeid = 0; // product number to be consumed
  6. Unsigned short in = 0; // buffer subscript when the product enters the Buffer Zone
  7. Unsigned short out = 0; // buffer subscript when the product outputs the Buffer Zone
  8. Int g_buffer [size_of_buffer]; // the buffer is a cyclic queue.
  9. Bool g_continue = true; // The control program ends.
  10. Handle g_hmutex; // used for mutual exclusion between threads
  11. Handle g_hfullsemaphore; // forces the producer to wait when the buffer is full.
  12. Handle g_hemptysemaphore; // force the consumer to wait when the buffer zone is empty
  13. DWORD winapi producer (lpvoid); // producer thread
  14. DWORD winapi consumer (lpvoid); // consumer thread
  15. Int main ()
  16. {
  17. // Create mutually exclusive Signals
  18. G_hmutex = createmutex (null, false, null );
  19. G_hfullsemaphore = createsemaphore (null, SIZE_OF_BUFFER-1, SIZE_OF_BUFFER-1, null );
  20. G_hemptysemaphore = createsemaphore (null, 0, SIZE_OF_BUFFER-1, null );
  21. // Adjust the values below to find that when the number of producers is greater than the number of consumers,
  22. // The production speed is fast, and the producer often waits for the consumer. On the contrary, the consumer often waits
  23. Const unsigned short producers_count = 3; // number of producers
  24. Const unsigned short consumers_count = 1; // number of consumers
  25. // Total number of threads
  26. Const unsigned short threads_count = producers_count + consumers_count;
  27. Handle hthreads [producers_count]; // handle of each thread
  28. DWORD producerid [consumers_count]; // producer thread identifier
  29. DWORD consumerid [threads_count]; // consumer thread ID
  30. // Create a producer thread
  31. For (INT I = 0; I <producers_count; ++ I ){
  32. Hthreads [I] = createthread (null, 0, producer, null, 0, & producerid [I]);
  33. If (hthreads [I] = NULL) Return-1;
  34. }
  35. // Create a consumer thread
  36. For (INT I = 0; I <consumers_count; ++ I ){
  37. Hthreads [producers_count + I] = createthread (null, 0, consumer, null, 0, & consumerid [I]);
  38. If (hthreads [I] = NULL) Return-1;
  39. }
  40. While (g_continue ){
  41. If (getchar () {// press enter to terminate the program running
  42. G_continue = false;
  43. }
  44. }
  45. Return 0;
  46. }
  47. // Produce a product. After a simple simulation, only the ID number of the new product is output.
  48. Void produce ()
  49. {
  50. STD: cerr <"producing" <++ productid <"...";
  51. STD: cerr <"succeed" <STD: Endl;
  52. }
  53. // Add the newly produced product to the buffer zone
  54. Void append ()
  55. {
  56. STD: cerr <"appending a product ...";
  57. G_buffer [in] = productid;
  58. In = (in + 1) % size_of_buffer;
  59. STD: cerr <"succeed" <STD: Endl;
  60. // Output the current status of the buffer
  61. For (INT I = 0; I <size_of_buffer; ++ I ){
  62. STD: cout <I <":" <g_buffer [I];
  63. If (I = In) STD: cout <"<-- production ";
  64. If (I = out) STD: cout <"<-- consume ";
  65. STD: cout <STD: Endl;
  66. }
  67. }
  68. // Extract a product from the buffer
  69. Void take ()
  70. {
  71. STD: cerr <"taking a product ...";
  72. Consumeid = g_buffer [out];
  73. Out = (out + 1) % size_of_buffer;
  74. STD: cerr <"succeed" <STD: Endl;
  75. // Output the current status of the buffer
  76. For (INT I = 0; I <size_of_buffer; ++ I ){
  77. STD: cout <I <":" <g_buffer [I];
  78. If (I = In) STD: cout <"<-- production ";
  79. If (I = out) STD: cout <"<-- consume ";
  80. STD: cout <STD: Endl;
  81. }
  82. }
  83. // Consume one product
  84. Void consume ()
  85. {
  86. STD: cerr <"consuming" <consumeid <"...";
  87. STD: cerr <"succeed" <STD: Endl;
  88. }
  89. // Producer
  90. DWORD winapi producer (lpvoid lppara)
  91. {
  92. While (g_continue ){
  93. Waitforsingleobject (g_hfullsemaphore, infinite );
  94. Waitforsingleobject (g_hmutex, infinite );
  95. Produce ();
  96. Append ();
  97. Sleep (1500 );
  98. Releasemutex (g_hmutex );
  99. Releasesemaphore (g_hemptysemaphore, 1, null );
  100. }
  101. Return 0;
  102. }
  103. // Consumer
  104. DWORD winapi consumer (lpvoid lppara)
  105. {
  106. While (g_continue ){
  107. Waitforsingleobject (g_hemptysemaphore, infinite );
  108. Waitforsingleobject (g_hmutex, infinite );
  109. Take ();
  110. Consume ();
  111. Sleep (1500 );
  112. Releasemutex (g_hmutex );
  113. Releasesemaphore (g_hfullsemaphore, 1, null );
  114. }
  115. Return 0;
  116. }

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.