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



我正试图用Win32函数QueueUserWorkItem()调用一个私有成员函数(不应作为公共或受保护)作为工作项。我知道我以前做过,这很容易,但现在我找不到那个片段,也无法使bind()巫毒发挥作用。因此,就这个问题而言,类是:

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作业应该是什么样子?

这似乎有效:

#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 %un", arg1, arg2);
    return;
}
void Arbitrary::PrivateWorkItem2(char * arg1)
{
    printf("String is %sn", 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;
}

试试类似的东西:

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
}

相关内容

  • 没有找到相关文章

最新更新