首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >避免使用多线程c++优化变量

避免使用多线程c++优化变量
EN

Stack Overflow用户
提问于 2016-04-02 11:53:14
回答 1查看 367关注 0票数 0

我正在用c++编写一个程序,我有一个线程在使用.detach();当我尝试从它访问另一个线程时,调试器说响应已经优化出来了,我无法访问它。有什么办法可以“解决”吗?谢谢

这里有代码:

代码语言:javascript
复制
int foos;
unsigned int ReadProcess(HANDLE phandle, unsigned int address) {
    unsigned int Pointer;
    ReadProcessMemory(phandle, (void*)(address), &Pointer, sizeof(address), 0);
    return Pointer;
}

void foo()
{
    while (1) {
        if (foos == 1) { break; }
        volatile unsigned int xPointer, xBaseaddress = 0x002CF7DC, xoffset = 0x448, xoffset1 = 0x198, xoffset2 = 0x498, xoffset3 = 0x268, xoffset4 = 0x24;
        HANDLE phandle = GetProcessHandle("Speed Calculator");
        DWORD Base = (DWORD)GetBaseAddress(phandle);
        xPointer = Base + xBaseaddress; //xPointer optimized out here
        xPointer = ReadProcess(phandle, xPointer) + xoffset; //and here
        xPointer = ReadProcess(phandle, xPointer) + xoffset1; //and here
        xPointer = ReadProcess(phandle, xPointer) + xoffset2; //and here
        xPointer = ReadProcess(phandle, xPointer) + xoffset3; //and here
        xPointer = ReadProcess(phandle, xPointer) + xoffset4; //and here
        if (!SendMessage(textBox1, WM_SETTEXT, NULL, (LPARAM)("IS: ") + xPointer))//and because of that, here
            MessageBox(0, "Error: " + GetLastError(), "Error", MB_OK);
        Sleep(299);
    }
}

void main() {

                foos = 0;
                std::thread first(foo);
                first.detach();

}

我忽略了我的部分代码,因为它主要是一个win32 api交互。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-02 12:18:37

已使用参数调用编译器,要求对代码进行优化。

Visual示例

查看Visual中的解决方案配置(可以选择ReleaseDebug的下拉列表)。

如果您更改为Debug,则不应该对变量进行优化,您可以看到正在发生的事情。

更改解决方案配置时所发生的情况是,Visual在调用编译器时为该配置设置参数。在这种情况下,我们倾向于使用/Od (Disabled)标志来表示Configuration properties→C/C++→Optimization→Optimization,而不是/O2 (Maximize Speed)

在调试期间,您还可以打开一个“线程”窗口来查看创建的线程。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36372877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档