新线程仅发出第一条指令,永远不会恢复
new Thread making only first instruction and never resumes
这是我的问题:
我正在创建线程
HANDLE Threads[THREAD_NUM]
启动线程
void LaunchThread (int i) {
*some checks if the handle is not null etc*;
DWORD threadId;
threads[i] = CreateThread (
NULL,
0,
(LPTHREAD_START_ROUTINE)*StaticMethodInvokingTheThreadFunc,
*StructPassingToThatMethod ( basically have a pointer and thread number)*,
0,
&threadId);
*DebugMessage "thread created"*;
*DebugMessage "with id = (threadId)"*;
}
静态方法调用函数
void DoSmth () {
*DebugMessage "thread func started"*;
...;
*then code with another messages all the way*;
}
等待功能简单
void WaitThread (i){
*DebugMessage "wait for thread to finish"*;
WaitForSingleObject (Threads[i], INFINITE);
}
在主函数中,序列:
LaunchThread (i);
WaitThread (i);
程序从不返回 WaitThread ()
函数,并且 (!) 消息列表如下所示
"thread created"
"thread func started" (and nothing done after that message in DoSmth () func)
"with id = .."
"wait for thread to finish"
即使有 1 个线程也会发生这种情况...对于几个线程,我只是有
for ( int t = 0; t < THREAD_NUM; t++)
LaunchThread (t);
以及等待线程的相同循环
为什么会这样?我试图ResumeThread ()
,关闭线程的手柄并重新启动它,但没有任何帮助。
可能是函数 StaticMethodInvokingTheThreadFunc 实际上没有 LPTHREAD_START_ROUTINE 的签名,但这被函数指针强制强制转换为它的事实所掩盖。取下石膏。如果编译器开始抱怨,则调整函数的签名。
可能是调用CreateThread在您的情况下是错误的。在99%的情况下都是错误的,你真的应该调用CRT库函数_beginthreadex
。这个函数在内部调用CreateThread,但它也做一些初始化工作。将 CreateThread 替换为 _beginthreadex。
可能是在打印文本"线程函数已启动"之后,执行在 StaticMethodInvokingTheThreadFunc 中挂起。修改函数中的代码,或在最后一个函数的行上放置断点并调试项目以查看是否会命中断点。
相关文章:
- 如何在 c++ 中确定一条指令(以字节为单位)在哪里结束,另一条指令从哪里开始?
- 如何在 Win32 中用一条线连接 2 个文本框?
- C++ 如何在同一行中和在一条线上?
- 如何在SDL2窗口上使用Cairo绘制一条白色直线
- 在Opengl中绘制一条不显示C++的线
- 为什么当 while 循环中的 if 条件变为 false 时,我的函数不输出最后一条语句?
- x86 - 为什么编译器在下一条指令中插入看似毫无意义的JMP?
- CMake似乎忽略了一条线
- 在C 中使用Legacy OpenGL(即时模式)绘制一条厚线
- 在图像标签上画一条线并计算距离
- switch 语句结束和下一条语句之间的时间是否具有确定性(与路径无关)
- 如何在OMNET++中收到来自前一个模块的所有消息后将一条消息发送到下一个模块
- 一条线和两行声明之间的差异
- char*与上一条指令中设置的值的比较未优化
- new、()和[]运算符都在一条指令中
- 是否可以在一条指令中将4uint8_t复制到4int16中
- 对布尔值的读/写保证是C/C++中的一条指令
- 在C/C++中,左移或右移零点实际上会生成一条指令
- 为什么添加一条从未执行过的指令会导致我的代码性能下降
- 您将如何编写可能优化为一条SSE指令的无符号加法代码