You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用_beginthreadex时参数类型转换失败问题求助

解决_beginthreadex线程函数类型不匹配问题

核心问题出在线程函数的调用约定不匹配:
_beginthreadex要求线程函数使用__cdecl调用约定,但你定义的threadWork用了WINAPI(这是__stdcall的宏别名),两种调用约定的函数指针类型不兼容,所以编译器报错。

直接按下面的方式修改即可:

修正后的线程函数

unsigned int __cdecl threadWork(void* threadNo)
{
    int threadID = *(int*)threadNo;
    for (unsigned int i = 0; i < numberIter; i++)
    {
        // 这里写你的线程执行逻辑
    }
    return 0; // 返回无符号整数作为线程退出码
}

调用代码无需修改

threadIDs[i] = i;
threads[i] = (HANDLE)_beginthreadex(NULL, 0, threadWork, &threadIDs[i], 0, NULL);

额外说明:

  • 如果你更习惯用WINAPI(__stdcall)调用约定,应该改用系统API CreateThread,它对线程函数的调用约定要求是WINAPI。
  • _beginthreadex是C运行时库提供的多线程创建函数,依赖__cdecl调用约定来处理C运行时的初始化/清理,这是它和CreateThread的关键区别之一。

内容的提问来源于stack exchange,提问作者Joshua Williams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 12:24:55