WINAPI:在类成员函数上创建新线程-不兼容的参数类型

时间:2019-02-27 16:48:35

标签: c++ winapi createthread

我一直在将一些相当程序化的C ++重新架构为更经典的东西。原始代码运行得很好,部分代码启动了一个新线程来进行一些文件清理。进行此清理的函数是新线程的入口点。请参见下面的代码。注意:该代码不会执行,但是显示了有效的原理。

#include <stdlib.h>

// Structure for passing application data to a new thread.
typedef struct threadData {
    networkShare*   netArchive;
    rig*            rigInfo;
    rigDatabase*    dbConn;
    networkConn*    netConn;
    char*           logBuffer;
} THREADDATA;

// Global handle to a mutex object
// Used to control access to the inter thread log buffer
HANDLE ghMutex;

DWORD WINAPI cleanLocalArchive(LPVOID lpParam) {
    THREADDATA* p_threadData = (THREADDATA*)lpParam;

    // ... Do stuff ...

    return <<something>>;
}

int main(int argc, char** argv) {
    // Variables for local archive thread
    HANDLE h_CleanerThread = 0;
    THREADDATA* p_threadData = NULL;
    DWORD dwThreadId;

    // Create a mutex with no initial owner
    ghMutex = CreateMutex(
        NULL,              // default security attributes
        FALSE,             // initially not owned
        NULL);             // unnamed mutex

    if (ghMutex == NULL) {
        printf("CreateMutex error: %d\n", GetLastError());
        return 1;
    }

    // Declare the data structure for passing app setting to a new Thread and populate it.
    p_threadData = DBG_NEW THREADDATA;
    p_threadData->netArchive    = &rigArchive;
    p_threadData->rigInfo       = &thisRig;
    p_threadData->logBuffer     = (char*)malloc(BUF_SIZE);
    p_threadData->dbConn        = &archiveDB;
    p_threadData->netConn       = &netConnection;

    // Initialise p_threadData->logBuffer in case we never put anything else in there.
    sprintf_s(p_threadData->logBuffer, BUF_SIZE, "");

    // Start a new thread
    h_CleanerThread = CreateThread(
        NULL,                   // default security attributes
        0,                      // use default stack size  
        cleanLocalArchive,      // thread function name
        p_threadData,           // argument to thread function 
        0,                      // use default creation flags 
        &dwThreadId);           // returns the thread identifier 


    // ... Do other stuff ...

    return 0;
}

我现在将代码重构为类,并且函数“ cleanLocalArchive”是应用程序类AircatFeeder的成员函数。当我在CreateThread()中调用该成员函数时,根据对代码的处理方式,会出现各种错误:

现状:致电line =“ cleanLocalArchive”

error C3867: 'AirCatFeeder::cleanLocalArchive': non-standard syntax; use '&' to create a pointer to member

所以我加了一个&号:call line =“&cleanLocalArchive,”

error C2276: '&': illegal operation on bound member function expression
error C2660: 'CreateThread': function does not take 5 arguments

在挠头和Google-Foo之后,我发现了this链接,希望可以解决该问题。当然,这说明了为什么。我在类外创建了包装函数,并尝试调用该函数。该函数及其调用如下:

函数调用:

// Start a new thread
    h_CleanerThread = CreateThread(
        NULL,                       // default security attributes
        0,                          // use default stack size  
        trampoline,         // thread function name
        p_threadData,               // argument to thread function 
        0,                          // use default creation flags 
        &dwThreadId);               // returns the thread identifier 

包装功能:

DWORD trampoline(LPVOID data) {
    AirCatFeeder* scheduler = static_cast<AirCatFeeder*>(data);
    return scheduler->cleanLocalArchive(data);
}

但是,我仍然遇到相同的问题/错误消息。无论我调用成员函数还是包装函数,IntelliSense仍然会报告类似的错误消息:

argument of type "DWORD(*)(LPVOID lpParam)" is incompatible with parameter of type "LPTHREAD_START_ROUTINE"

希望现在很清楚我要实现的目标。有人可以教育我做错了什么吗?谢谢。

1 个答案:

答案 0 :(得分:1)

Answer courtesy of Hans Passant. See Comments on question:

The trampoline is missing WINAPI in its definition.