How to get Windows thread pool to call class member function?

☆樱花仙子☆ 提交于 2019-12-24 00:47:19

问题


I want the Windows thread pool (QueueUserWorkItem()) to call my class' member functions.

Unfortunately this cannot be done directly by passing a member function pointer as an argument to QueueUserWorkItem().

What makes it difficult is that more than one member function must be callable and they have different signatures (all return void though).

One probably need to add a few layers of abstraction to get this to work, but I'm not sure how to approach this. Any ideas?


回答1:


This might help. You can use tr1::function () and tr1::bind to "coalesce" various calls:

   #include <iostream>
   #include <tr1/functional>
   using namespace std;
   using namespace tr1;

   class A
   {
   public:
      void function(int i) { cout << "Called A::function with i=" << i << endl; }
   };

   void different_function(double c) {
       cout << "Called different_function with c=" << c << endl;
   }


   int main(int argc, char* argv[])
   {
      function<void()> f = bind(different_function, 3.14165);
      f();

      A a;
      f = bind(&A::function, a, 10);
      f();

      return 0;
   }

The address of the function object can be passed as a single callable object (needing only one address).




回答2:


Example: In your class add:

char m_FuncToCall;

    static DWORD __stdcall myclass::ThreadStartRoutine(LPVOID myclassref)
    {
      myclass* _val =  (myclass*)myclassref;
      switch(m_FuncToCall)
      {
         case 0:
          _val->StartMyOperation();
         break;
       }
       return 0;
    }

Make a member for adding to queue then

void myclass::AddToQueue(char funcId)
 {
    m_FuncToCall=funcId;
   QueueUserWorkItem(ThreadStartRoutine,this,WT_EXECUTEDEFAULT);
 }

or create

typedef void (*MY_FUNC)(void);
    typedef struct _ARGUMENT_TO_PASS
    {
     myclass* classref;
     MY_FUNC func;

    }ARGUMENT_TO_PASS;

and then

void myclass::AddToQueue(MY_FUNC func)
{
   ARGUMENT_TO_PASS _arg;
   _arg.func = func;
   _arg.classref = this;
    QueueUserWorkItem(ThreadStartRoutine,&_arg,WT_EXECUTEDEFAULT);
}

If you need further explanation feel free to ask :)

EDIT: You'll need to change the ThreadStartRoutine for the second example and you can also change the struct to hold the passing argument



来源:https://stackoverflow.com/questions/6666259/how-to-get-windows-thread-pool-to-call-class-member-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!