#defineUnicode#include<stdio.h>#include<tchar.h>#include<windows.h>#include<process.h>BOOL buseing=false;unsignedint__stdcall ThreadRun (void*LParam) { intNnum =0; while(true) { if(!buseing) {buseing= TRUE;//lockedprintf"threadrun:%d\r\n", nnum++);//Make sure the use is complete before it is pausedBuseing =FALSE; }}}unsignedint__stdcall Threadmonitor (void*LParam) {HANDLE Hthread=(HANDLE) (LParam); while(true) {context context; Context. Contextflags=Context_all; //pausing a threadSuspendThread (hthread); GetThreadContext (Hthread,&context); if(!buseing) {buseing= TRUE;//lockedprintf ("eax:0x%x esp:0x%x eip:0x%x\r\n", context. Eax,context. Esp,context. EIP); Buseing=FALSE; } //Start ThreadResumeThread (hthread); }}intMain () {HANDLE hthread[2]; hthread[0] = (HANDLE) _beginthreadex (nullptr,0, Threadrun,nullptr,0, nullptr); hthread[1] = (HANDLE) _beginthreadex (nullptr,0, threadmonitor,hthread[0],0, nullptr); WaitForMultipleObjects (sizeof(hthread)/sizeof(HANDLE), hthread,true, INFINITE); for(inti =0; i<sizeof(hthread)/sizeof(HANDLE); + +i) {CloseHandle (hthread[i]); } return 0;}
015 CONTEXT Thread-safe lock code implementation