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
- #include <windows.h>
- #include <stdio.h>
- #define SEND_THREAD_ID 1
- #define RECV_THREAD_ID 2
- Const UINT Uitimerid = 10;
- HANDLE Mainhandle;
- CONTEXT Send_context;
- CONTEXT Recv_context;
- HANDLE Hd_send_thread;
- HANDLE Hd_recv_thread;
- If Hd_send_thread is running, thread_id = 1;
- If Hd_recv_thread is running, thread_id = 2;
- int cur_thread_id = 0;
- void Send_thread_func ()
- {
- static int send_msg_count = 0;
- While (1)
- {
- printf ("Send_msg_count: ................%d\n.......
- Sleep (1000);
- }
- }
- void Recv_thread_func ()
- {
- static int recv_msg_count = 0;
- While (1)
- {
- printf ("Recv_msg_count:--------------------%d\n", recv_msg_count++);
- Sleep (1000);
- }
- }
- VOID CALLBACK Timerfun (hwnd hwnd, uint A, uint B, DWORD c)
- {
- static int ncount = 0;
- printf ("Timerfun ncount:%d\n", ncount++);
- if (ncount = = 0)
- {
- Send_context. Contextflags = Context_full;
- Recv_context. Contextflags = Context_full;
- GetThreadContext (Mainhandle, &send_context); //Save main thread context
- GetThreadContext (Mainhandle, &recv_context); //Save main thread context
- return;
- }
- if (ncount% 2 = = 1) //start Hd_send_thread
- {
- if (cur_thread_id! = send_thread_id)
- {
- SuspendThread (Mainhandle); //Abort the run of the main thread and simulate interrupts. But no register is saved
- Recv_context. Contextflags = Context_full;
- GetThreadContext (Mainhandle, &recv_context); //Save main thread context
- SetThreadContext (Mainhandle, &send_context); //Get the main thread context and prepare for switching tasks
- cur_thread_id = send_thread_id;
- ResumeThread (Mainhandle);
- }
- }
- Else //start Hd_recv_thread
- {
- if (cur_thread_id! = recv_thread_id)
- {
- SuspendThread (Mainhandle); //Abort the run of the main thread and simulate interrupts. But no register is saved
- Send_context. Contextflags = Context_full;
- GetThreadContext (Mainhandle, &send_context); //Save main thread context
- SetThreadContext (Mainhandle, &recv_context); //Get the main thread context and prepare for switching tasks
- cur_thread_id = recv_thread_id;
- ResumeThread (Mainhandle);
- }
- }
- }
- int main ()
- {
- MSG msg;
- SetTimer (NULL, Uitimerid, Timerfun);
- Hd_send_thread = CreateThread (NULL,
- 0,
- (Lpthread_start_routine) Send_thread_func,
- 0,
- 0,
- 0);
- SuspendThread (Hd_send_thread);
- Hd_recv_thread = CreateThread (NULL,
- 0,
- (Lpthread_start_routine) Recv_thread_func,
- 0,
- 0,
- 0);
- SuspendThread (Hd_recv_thread);
- DuplicateHandle (GetCurrentProcess (),
- Hd_send_thread,
- GetCurrentProcess (),
- &mainhandle,
- 0,
- TRUE,
- 2);
- cur_thread_id = send_thread_id;
- ResumeThread (Hd_send_thread);
- ResumeThread (Hd_recv_thread);
- While (GetMessage (&msg, NULL, 0, 0))
- {
- TranslateMessage (&MSG);
- DispatchMessage (&MSG);
- }
- return 0;
- }
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)