Multithreaded Programming (Context)

Source: Internet
Author: User

Reprint please indicate the source of the original, http://www.cnblogs.com/flyingcloude/p/6992343.html

By switching the context content of a thread, a single-threaded simulation of multi-threaded runs is used. The APIs that you need to use in your program are mainly the following:

GetThreadContext (HANDLE hthread, Context lpcontext);

Description: GetThreadContext, saving the context of Hthread to Lpcontext

SetThreadContext (HANDLE hthread, const Context *lpcontext);

Description: SetThreadContext, saving the contents of Lpcontext to the context of hthread.

[CPP]View PlainCopy
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #define SEND_THREAD_ID 1
  4. #define RECV_THREAD_ID 2
  5. Const UINT Uitimerid = 10;
  6. HANDLE Mainhandle;
  7. CONTEXT Send_context;
  8. CONTEXT Recv_context;
  9. HANDLE Hd_send_thread;
  10. HANDLE Hd_recv_thread;
  11. If Hd_send_thread is running, thread_id = 1;
  12. If Hd_recv_thread is running, thread_id = 2;
  13. int cur_thread_id = 0;
  14. void Send_thread_func ()
  15. {
  16. static int send_msg_count = 0;
  17. While (1)
  18. {
  19. printf ("Send_msg_count: ................%d\n.......
  20. Sleep (1000);
  21. }
  22. }
  23. void Recv_thread_func ()
  24. {
  25. static int recv_msg_count = 0;
  26. While (1)
  27. {
  28. printf ("Recv_msg_count:--------------------%d\n", recv_msg_count++);
  29. Sleep (1000);
  30. }
  31. }
  32. VOID CALLBACK Timerfun (hwnd hwnd, uint A, uint B, DWORD c)
  33. {
  34. static int ncount = 0;
  35. printf ("Timerfun ncount:%d\n", ncount++);
  36. if (ncount = = 0)
  37. {
  38. Send_context. Contextflags = Context_full;
  39. Recv_context. Contextflags = Context_full;
  40. GetThreadContext (Mainhandle, &send_context); //Save main thread context
  41. GetThreadContext (Mainhandle, &recv_context); //Save main thread context
  42. return;
  43. }
  44. if (ncount% 2 = = 1) //start Hd_send_thread
  45. {
  46. if (cur_thread_id! = send_thread_id)
  47. {
  48. SuspendThread (Mainhandle); //Abort the run of the main thread and simulate interrupts. But no register is saved
  49. Recv_context. Contextflags = Context_full;
  50. GetThreadContext (Mainhandle, &recv_context); //Save main thread context
  51. SetThreadContext (Mainhandle, &send_context); //Get the main thread context and prepare for switching tasks
  52. cur_thread_id = send_thread_id;
  53. ResumeThread (Mainhandle);
  54. }
  55. }
  56. Else //start Hd_recv_thread
  57. {
  58. if (cur_thread_id! = recv_thread_id)
  59. {
  60. SuspendThread (Mainhandle); //Abort the run of the main thread and simulate interrupts. But no register is saved
  61. Send_context. Contextflags = Context_full;
  62. GetThreadContext (Mainhandle, &send_context); //Save main thread context
  63. SetThreadContext (Mainhandle, &recv_context); //Get the main thread context and prepare for switching tasks
  64. cur_thread_id = recv_thread_id;
  65. ResumeThread (Mainhandle);
  66. }
  67. }
  68. }
  69. int main ()
  70. {
  71. MSG msg;
  72. SetTimer (NULL, Uitimerid, Timerfun);
  73. Hd_send_thread = CreateThread (NULL,
  74. 0,
  75. (Lpthread_start_routine) Send_thread_func,
  76. 0,
  77. 0,
  78. 0);
  79. SuspendThread (Hd_send_thread);
  80. Hd_recv_thread = CreateThread (NULL,
  81. 0,
  82. (Lpthread_start_routine) Recv_thread_func,
  83. 0,
  84. 0,
  85. 0);
  86. SuspendThread (Hd_recv_thread);
  87. DuplicateHandle (GetCurrentProcess (),
  88. Hd_send_thread,
  89. GetCurrentProcess (),
  90. &mainhandle,
  91. 0,
  92. TRUE,
  93. 2);
  94. cur_thread_id = send_thread_id;
  95. ResumeThread (Hd_send_thread);
  96. ResumeThread (Hd_recv_thread);
  97. While (GetMessage (&msg, NULL, 0, 0))
  98. {
  99. TranslateMessage (&MSG);
  100. DispatchMessage (&MSG);
  101. }
  102. return 0;
  103. }

I do not know why in the main function, the context of Hd_send_thread, hd_recv_thread two threads are saved to Send_context, Recv_context, and then to the timer interrupt to switch the error, So this part of the code is also placed in the first time when the timer interrupts to run.

Reprint please indicate the source of the original, http://www.cnblogs.com/flyingcloude/p/6992343.html

Multithreaded Programming (Context)

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.