Timer and multithreading settimer and multi-thread each thread uses a timer independently

Source: Internet
Author: User
A few days ago, a student asked me to help him develop the producer-consumer simulation program in the operating system course. The requirement is as follows: encapsulate the producer and consumer simulation algorithms in a dynamic link library, the main program calls related functions. The speed at which the producer places the product and the consumer to remove the product can be adjusted. Use the cyclic queue and stack respectively. This algorithm is used to simulate production. The consumer opens a thread to access a shared buffer synchronously. However, the requirement can adjust the speed. My idea is to create a timer in each thread, but in windows, the Timer feature is: Every scheduled time, in Windows, A wm_timer message is put into the message queue of the application. So my solution is as follows:
  1. /* Change the timer message */
  2. # Define wm_settimer wm_user+ 100
  3. /* Producer thread function */
  4. DWORD winapi producerfunc (lpvoid lpparameter)
  5. {
  6. MSG;
  7. Uint producertimerid;
  8. /* Create a message queue for this thread */
  9. Peekmessage (& MSG, null, 0, 0, pm_noremove );
  10. Producertimerid = settimer (null, 0, g_uproducertimer, null );
  11. While (getmessage (& MSG, null, 0, 0 ))
  12. {
  13. If (msg. Message = wm_timer)
  14. {
  15. Waitforsingleobject (g_hemptysemaphore, infinite );
  16. Waitforsingleobject (g_hmutex, infinite );
  17. Producer ();
  18. Releasemutex (g_hmutex );
  19. Releasesemaphore (g_hfullsemaphore, 1, null );
  20. }
  21. Else if (msg. Message = wm_settimer)
  22. {
  23. Killtimer (null, producertimerid );
  24. Producertimerid = settimer (null, 0, g_uproducertimer, null );
  25. }
  26. Else
  27. {
  28. Translatemessage (& MSG );
  29. Dispatchmessage (& MSG );
  30. }
  31. }
  32. Killtimer (null, producertimerid );
  33. Return 0;
  34. }
  35. /* Consumer thread function */
  36. DWORD winapi consumerfunc (lpvoid lpparameter)
  37. {
  38. MSG;
  39. Uint consumertimerid;
  40. Peekmessage (& MSG, null, 0, 0, pm_noremove );
  41. Consumertimerid = settimer (null, 0, g_uconsumertimer, null );
  42. While (getmessage (& MSG, null, 0, 0 ))
  43. {
  44. If (msg. Message = wm_timer)
  45. {
  46. Waitforsingleobject (g_hfullsemaphore, infinite );
  47. Waitforsingleobject (g_hmutex, infinite );
  48. Consumer ();
  49. Releasemutex (g_hmutex );
  50. Releasesemaphore (g_hemptysemaphore, 1, null );
  51. }
  52. Else if (msg. Message = wm_settimer)
  53. {
  54. Killtimer (null, consumertimerid );
  55. Consumertimerid = settimer (null, 0, g_uconsumertimer, null );
  56. }
  57. Else
  58. {
  59. Translatemessage (& MSG );
  60. Dispatchmessage (& MSG );
  61. }
  62. }
  63. Killtimer (null, consumertimerid );
  64. Return 0;
  65. }

We hope you can provide a better solution.

Complete source code here: http://download.csdn.net/source/880262

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.