【问题标题】:_beginthread/ex C3861 - identifier not found_beginthread/ex C3861 - 未找到标识符
【发布时间】:2013-06-12 15:06:18
【问题描述】:

这是我的测试代码

#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 *)'    

【问题讨论】:

    标签: c++ windows visual-studio-2008


    【解决方案1】:

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

    在 cdecl 和 stdcall 不同的 x86 上,您不能同时使用 _beginthread_beginthreadex 的单线程过程(在 x64 和 ARM 上,只有一个调用约定,因此 stdcall 和 cdecl 表示相同的意思并且没有必要)。

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

    【讨论】:

      【解决方案2】:

      你是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>
      

      【讨论】:

      • 现在这个Error 2 error C2664: '_beginthread' : cannot convert parameter 1 from 'unsigned int (__stdcall *)(void *)' to 'void (__cdecl *)(void *)'
      • 该错误似乎不言自明,与此问题无关。你可以提出一个新问题来解决这个问题。
      【解决方案3】:

      docs 表明它在 process.h 中,所以你需要

      #include <process.h>
      

      请注意,"" 搜索到 &lt;&gt; 的不同路径

      【讨论】:

        【解决方案4】:

        让您的threadFunc 匹配所需的签名! (将 __stdcall 替换为 __cdecl )并使其无效...直到匹配为止

        【讨论】:

        • _beginthread(threadFunc, 0, NULL );: Error 1 error C2664: '_beginthread' : cannot convert parameter 1 from 'unsigned int (__cdecl *)(void *)' to 'void (__cdecl *)(void *)'
        【解决方案5】:

        或者在 Boost 库中的包含路径阴影 &lt;process.h&gt; 的某个位置上可能有另一个来自不同库的 &lt;process.h&gt; 标头。

        【讨论】:

          猜你喜欢
          • 2013-04-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多