c++ - _beginthread/ex C3861 - 找不到标识符

标签 c++ windows visual-studio-2008

这是我的测试代码

#include "stdafx.h"
#include "windows.h"
#include "iostream" 
using namespace std;

HANDLE hMutex;

static unsigned __stdcall threadFunc(void *params)
{
    WaitForSingleObject(hMutex,INFINITE);
    printf(":D:D:D\n");
    ReleaseMutex(hMutex);
    return NULL;    
}

int _tmain(int argc, _TCHAR* argv[])
{
        hMutex=CreateMutex(NULL,FALSE,NULL);
        //first try
        unsigned dwChildId;
        _beginthreadex(NULL, 0, &threadFunc, NULL, 0, &dwChildId);
        //second try
        _beginthread(threadFunc, 0, NULL );
        WaitForSingleObject(hMutex,INFINITE);
        printf("HD\n");
        ReleaseMutex(hMutex);
        int i;
        cin >> i;
    return 0;
}

给我 2 个错误:

Error   1   error C3861: '_beginthreadex': identifier not found 
Error   2   error C3861: '_beginthread': identifier not found   

我正在使用 MFC 作为共享 DLL。我也不知道如何创建两个具有相同功能的线程。

在我包含“process.h”之后

Error   2   error C2664: '_beginthread' : cannot convert parameter 1 from 'unsigned int (__stdcall *)(void *)' to 'void (__cdecl *)(void *)'    

最佳答案

_beginthread_beginthreadex 需要不同类型的函数。 _beginthread需要一个cdecl函数; _beginthreadex 需要一个 stdcall 函数。

在 x86 上,cdecl 和 stdcall 是不同的,您不能同时使用带有 _beginthread_beginthreadex 的单线程过程(在 x64 和 ARM 上,只有一个调用约定,所以 stdcall 和 cdecl 意思相同,不是必需的)。

就是说:不要使用 _beginthread。相反,请使用 _beginthreadex,并确保关闭它返回的句柄。 The documentation充分解释了 _beginthread 的缺点以及为什么 _beginthreadex 更可取。

关于c++ - _beginthread/ex C3861 - 找不到标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17068914/

相关文章:

c++ - cmake + sdl - 禁用 sdlmain

python - 如何在Python中读取unicode文件名?

c++ - 将控制台应用程序转换为 Windows 应用程序

c++ - 如何停止控制台应用程序中的闪烁?

c++ - 调试-进入操作系统功能?

python - 从 Python 接收 C++ 中的字符串

java - 在 Windows 上使用 Java 检测 Samba 共享上的文件系统更改

windows - 每次启动 git-bash 时都需要发出 ssh-agent 命令

visual-studio-2008 - 如何确定哪些 C/C++ 对象使用最多内存

sql-server - 服务器资源管理器不可用 (VS2008Pro)