#define UNICODE
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <process.h>
BOOL bUseing = FALSE;
unsigned int __stdcall ThreadRun(void* lParam)
{
int nNum = 0;
while(true)
{
if(!bUseing)
{
bUseing = TRUE; //上锁
printf("ThreadRun:%d\r\n",nNum++); //确保使用完成后才被暂停
bUseing = FALSE;
}
}
}
unsigned int __stdcall ThreadMonitor(void* lParam)
{
HANDLE hThread = (HANDLE)(lParam);
while(true)
{
CONTEXT context;
context.ContextFlags = CONTEXT_ALL;
//暂停线程
SuspendThread(hThread);
GetThreadContext(hThread,&context);
if(!bUseing)
{
bUseing = TRUE; //上锁
printf("EAX:0x%x ESP:0X%x EIP:0x%x\r\n",context.Eax,context.Esp,context.Eip);
bUseing = FALSE;
}
//开始线程
ResumeThread(hThread);
}
}
int main()
{
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(int i = 0; i<sizeof(hThread)/sizeof(HANDLE);++i)
{
CloseHandle(hThread[i]);
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步