以下是我的测试代码
#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;
}给了我两个错误:
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 *)' 发布于 2013-06-12 23:27:17
_beginthread和_beginthreadex需要不同类型的函数。_beginthread需要cdecl函数;_beginthreadex需要stdcall函数。
在x86上,cdecl和stdcall是不同的,您不能同时对_beginthread和_beginthreadex使用单线程过程(在x64和ARM上,只有一个调用约定,所以stdcall和cdecl的含义是相同的,不是必需的)。
也就是说:不要使用_beginthread。相反,使用_beginthreadex,并确保关闭它返回的句柄。The documentation充分解释了_beginthread的缺点以及_beginthreadex更可取的原因。
发布于 2013-06-12 23:12:12
你是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>发布于 2013-06-12 23:12:58
docs建议它在process.h中,所以您需要
#include <process.h>请注意,""会搜索到<>的不同路径
https://stackoverflow.com/questions/17068914
复制相似问题