这是我的测试代码
#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/