使用私有成员函数调用 Win32 QueueUserWorkItem()

Calling Win32 QueueUserWorkItem() with a private member function

我正在尝试调用私有成员函数(不应作为 public 或受保护)作为具有 Win32 函数 QueueUserWorkItem() 的工作项。我知道我以前做过这个并且很容易,但现在我找不到那个片段,我也无法使 bind() voodoo 起作用。因此,就此问题而言,class 是:

class Arbitrary {

    public:
        Arbitrary() ;
        ~Arbitrary() ;

        bool    UsefulPublicFunction(unsigned uParameter) ;

    protected:

    private:
        void    PrivateWorkItem(void* pVoid) ;
} ;

UsefulPublicFunction() 中我们可能会看到:

LPTHREAD_START_ROUTINE pThreadStartRoutine ;
ULONG uFlags = 0 ;
void* pContext = nullptr ;

if (QueueUserWorkItem(pThreadStartRoutine, pContext, uFlags)) {
    //blah blah blah
}

我似乎在杂草丛生的地方是对 pThreadStartRoutine 的分配,例如:

pThreadStartRoutine = std::bind<&Arbitrary::PrivateWorkItem, this, std::placeholders::_1> ;

我知道 PrivateWorkItem 的签名可能应该更改为:

private:
    DWORD   WINAPI  PrivateWorkItem(void* pVoid) ;

即使有了那个改变,也没有快乐。 VS2015 真的很讨厌我使用 bind().

的方式

我对 pThreadStartRoutine 的分配应该是什么样的?

尝试更像这样的东西:

class Arbitrary {

    public:
        Arbitrary() ;
        ~Arbitrary() ;

        bool UsefulPublicFunction(unsigned uParameter);

    protected:

    private:
        static DWORD WINAPI PrivateWorkItem(void* pVoid) ;
        void PrivateFunction();
} ;

DWORD WINAPI Arbitrary::PrivateWorkItem(void* pVoid)
{
    static_cast<Arbitrary*>(pVoid)->PrivateFunction();
    return 0;
}

...

if (QueueUserWorkItem(&PrivateWorkItem, this, 0)) {
    //blah blah blah
}

这似乎有效:

#include <Windows.h>

#include <stdio.h>

#include <functional>

using namespace std::placeholders;

class Arbitrary {

    public:

        bool UsefulPublicFunction(int uParameter);

    protected:

    private:
        typedef std::function<void (void)> CallbackType;
        static DWORD WINAPI ProcessWorkItem(void* pVoid);
        void PrivateWorkItem1(int arg1, int arg2);
        void PrivateWorkItem2(char * arg1);
};

void Arbitrary::PrivateWorkItem1(int arg1, int arg2)
{
    printf("Numbers are %u %u\n", arg1, arg2);
    return;
}

void Arbitrary::PrivateWorkItem2(char * arg1)
{
    printf("String is %s\n", arg1);
    return;
}

DWORD WINAPI Arbitrary::ProcessWorkItem(void* pVoid)
{
    CallbackType * callback = static_cast<CallbackType *>(pVoid);
    (*callback)();
    delete callback;
    return 0;
}

bool Arbitrary::UsefulPublicFunction(int param1)
{
    QueueUserWorkItem(&ProcessWorkItem, new CallbackType(std::bind(&Arbitrary::PrivateWorkItem1, this, param1, 7)), 0);

    QueueUserWorkItem(&ProcessWorkItem, new CallbackType(std::bind(&Arbitrary::PrivateWorkItem2, this, (char *)"This is my string")), 0);

    Sleep(1000);
    return true;
}

int main(int argc, char ** argv)
{
    Arbitrary x;

    x.UsefulPublicFunction(5);

    return 0;
}