zoukankan      html  css  js  c++  java
  • GetThreadContext和SetThreadContext

    #include "stdafx.h" 
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    DWORD WINAPI ThreadProc(LPVOID lpParamter)
    {
        for (int i = 0;i<10;i++)
        {
            Sleep(1000);
            printf("ThreadProc1********* %d\n",i);
        }
        return 0;
    }
    
    DWORD WINAPI ThreadProc1(LPVOID lpParamter)
    {
        for (int i = 0;i<50;i++)
        {
            Sleep(100);
            printf("ThreadProc1********* %d\n",i);
        }
        return 0;
    }
    
    DWORD WINAPI ThreadProc2(LPVOID lpParamter)
    {
        for (int j = 0;j<50;j++)
        {
            Sleep(50);
            printf("ThreadProc2********* %d\n",j);
        }
        return 5;
    }
    
    int main()
    {
        //unsigned long ulThreadId = 0;
        HANDLE hThread[2];
        DWORD dwr1;
        DWORD dwr2;
    
    
        hThread[0] = CreateThread(NULL, 0, ThreadProc1, NULL, 0, NULL);
        //hThread[1] = CreateThread(NULL, 0, ThreadProc2, NULL, 0, NULL);
    
        //Sleep(2000);
        SuspendThread(hThread[0]);
    
        CONTEXT context;
        context.ContextFlags = CONTEXT_INTEGER;

      //从线程获取寄存器的值存入context GetThreadContext(hThread[
    0],&context); printf("%x -- %x\n",context.Eax,context.Ecx); context.Eax = 1; context.Ecx = 2;

      //把修改好的context传入thread SetThreadContext(hThread[
    0],&context); printf("%x -- %x\n",context.Eax,context.Ecx); GetThreadContext(hThread[0],&context); printf("%x -- %x\n",context.Eax,context.Ecx); ResumeThread(hThread[0]); // WaitForMultipleObjects(2,hThread,TRUE,INFINITE); // GetExitCodeThread(hThread[0],&dwr1); // GetExitCodeThread(hThread[1],&dwr2); cout << "线程结束了!" <<endl; getchar(); CloseHandle(hThread[0]); CloseHandle(hThread[1]); //system("pause"); return 0; }
  • 相关阅读:
    天猫弹性导航栏
    php 中构造函数和析构函数
    web服务器集群(多台web服务器)后session如何同步和共享
    mycat中schema.xml的一些解释
    mycat高可用集群搭建
    mycat水平分表
    mycat实现mysql数据库的垂直切分
    高并发、大流量解决方案
    nginx负载均衡六种策略
    mysql主从复制实现
  • 原文地址:https://www.cnblogs.com/ganxiang/p/13140624.html
Copyright © 2011-2022 走看看