这是我的测试代码
#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 *)'
答案 0 :(得分:2)
_beginthread
和_beginthreadex
需要不同类型的功能。 _beginthread
需要cdecl函数; _beginthreadex
需要stdcall函数。
在x86上,cdecl和stdcall不同,你不能对_beginthread
和_beginthreadex
使用单个线程程序(在x64和ARM上,只有一个调用约定,所以stdcall和cdecl意思是相同的,不是必要的。)
说:不要使用_beginthread
。相反,请使用_beginthreadex
,并确保关闭它返回的句柄。 The documentation充分解释了_beginthread
的缺点以及为什么_beginthreadex
更为可取。
答案 1 :(得分:1)
你是missing the appropriate header and/or you're not using the multithreaded C run-time libraries。
/* Routine: _beginthreadex
* Required header: process.h
*/
#include <process.h>
答案 2 :(得分:1)
答案 3 :(得分:1)
让您的threadFunc
匹配所需的签名! (将__stdcall替换为__cdecl)并使其无效......直到匹配为止
答案 4 :(得分:0)
或者,在Boost库中包含路径阴影<process.h>
的某个地方,可能有另一个<process.h>
标头。