如何获取Windows线程池来调用类成员函数? [英] How to get Windows thread pool to call class member function?

查看:200
本文介绍了如何获取Windows线程池来调用类成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要Windows线程池(QueueUserWorkItem())来调用我的类的成员函数。



不幸的是,这不能直接通过传递一个成员函数指针一个参数QueueUserWorkItem()。



很难的是,多个成员函数必须是可调用的,并且它们有不同的签名(全部返回void)。

一个可能需要添加几层抽象来使这个工作,但我不知道如何处理这个。任何想法?

解决方案

这可能有所帮助。
您可以使用tr1 :: function()和tr1 :: bindcoalesce各种调用:

  include< iostream> 
#include< tr1 / functional>
using namespace std;
使用命名空间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;
}

函数对象的地址可以作为单个可调用对象只有一个地址)。


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

这篇关于如何获取Windows线程池来调用类成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆