开发者

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

开发者 https://www.devze.com 2023-03-19 12:18 出处:网络
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

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?


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).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号