1

これが私のテストコードです

#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 として使用しています。また、同じ機能を持つ 2 つのスレッドを作成する方法もわかりません。

「process.h」を含めた後

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

5 に答える 5

2

_beginthread_beginthreadexさまざまなタイプの機能が必要です 。_beginthreadcdecl 関数が必要です。_beginthreadexstdcall 関数が必要です。

cdecl と stdcall が異なる x86 では、単一のスレッド プロシージャを と の両方_beginthreadで使用することはできません_beginthreadex(x64 と ARM では、呼び出し規約が 1 つしかないため、stdcall と cdecl は同じ意味であり、必要ありません)。

つまり、使用しないでください_beginthread。代わりに、 を使用_beginthreadexし、それが返すハンドルを必ず閉じてください。 ドキュメントは、の欠点と_beginthreadその理由を適切に説明してい_beginthreadexます。

于 2013-06-12T15:27:16.913 に答える
1

適切なヘッダーがないか、マルチスレッド C ランタイム ライブラリを使用していません。

/* Routine: _beginthreadex
 * Required header: process.h
 */
#include <process.h>
于 2013-06-12T15:12:11.847 に答える
1

あなたのthreadFunc試合を必要な署名にしてください!( __stdcall を __cdecl に置き換えます)そしてそれを無効にします...一致するまで

于 2013-06-12T15:21:23.900 に答える
0

または、Boost ライブラリからの<process.h>インクルード パス シャドウイングのどこかに、別のライブラリからの別のヘッダーが存在する可能性があります。<process.h>

于 2014-10-09T12:20:03.997 に答える