存储并稍后调用未知类的成员函数
我正在尝试创建一个可以运行未知类中的函数的线程池。我不希望必须创建非成员作为代理。 我已经成功创建了一个工作池和工作线程类和任务结构,所有这些都是模板。
// ThreadPool.h
/* Threadpool creates N WorkerThreads (each worker has a ptr to the creating pool),
these block until a task is ready then call ThreadPool::doTask() */
template<class T>
struct Task {
Task() : func(0), inst(0) { }
Task(boost::function<void(T*)> function, T* instance) : func(0), inst(0) {
func = function;
inst = instance;
}
void operator()() {
Task::func(inst);
}
T* inst;
boost::function<void(T*)> func;
};
template<class T>
class ThreadPool {
template<class T> friend class WorkerThread;
public:
void addTask(Task<T> task) {
... // Some stuff
}
bool doTask() {
Task<T> task;
... // Gets a task from std::queue
// Check the task actually exists!
if(task.func && task.inst) {
// Do the task
(task)();
}
}
private:
std::queue<Task<T>> mTasks;
};
事实上,只要我确定 ThreadPool 和 Task 的类,这段代码就可以工作。但我希望能够调用未知类类型的成员。我曾考虑过使用 void ptr,但找不到将其转换为有效实例 ptr 的方法。我也研究过 boost::mem_fun 但很难真正掌握它。
我已经简要阅读了有关 C++0x 的内容,据我了解,它应该使解决我的问题变得更容易,但如果可能的话,我想在此之前解决这个问题。
I am trying to create a threadpool that can run functions from unknown classes. I do not wish to have to create non-members as a proxy.
I have managed to create a working pool & workerthread class and a task structure, all of these are templates.
// ThreadPool.h
/* Threadpool creates N WorkerThreads (each worker has a ptr to the creating pool),
these block until a task is ready then call ThreadPool::doTask() */
template<class T>
struct Task {
Task() : func(0), inst(0) { }
Task(boost::function<void(T*)> function, T* instance) : func(0), inst(0) {
func = function;
inst = instance;
}
void operator()() {
Task::func(inst);
}
T* inst;
boost::function<void(T*)> func;
};
template<class T>
class ThreadPool {
template<class T> friend class WorkerThread;
public:
void addTask(Task<T> task) {
... // Some stuff
}
bool doTask() {
Task<T> task;
... // Gets a task from std::queue
// Check the task actually exists!
if(task.func && task.inst) {
// Do the task
(task)();
}
}
private:
std::queue<Task<T>> mTasks;
};
As is, this code works, providing I determine the class for ThreadPool and Task. But I want to be able to call members of unknown class types. I had considered a void ptr but I could not find a way to convert this to a valid instance ptr. I have also looked into boost::mem_fun but struggled to really get to grips with it.
I have briefly read about C++0x and from what I understand, it should make solving my problem easier but I would like to solve this before then, if at all possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要使用 T*,而不是仅仅使用
boost::function
?这样您就可以使用自由函数和成员函数,并且可以简化代码。
类 X 实例上的成员的任务可以像这样排队:
代码中没有强制转换和模板。
更新:
使用 boost::function 而不是您的 Task 类。然后,您只需跟踪实例并根据需要调用它们即可。例如:
请注意,通过将此方法与 boost::shared_ptr 结合使用,当函数超出范围时(如果它是最后一个引用),您可以自动销毁对象。这让生活变得更加轻松。
Why use a T* at all, instead of just
boost::function<void ()>
?That way you can use free functions as well as member functions, and you can simplify your code.
A task for a member on an instance of class X could be queued like this:
With no casts and no templates in your code.
Update:
Use boost::function instead of your Task class. You then just need to keep track of the instances and call them as appropriate. For example:
Note that by combining this method with boost::shared_ptr, you get automatic destruction of your objects when the function goes out of scope, if it's the last reference. That makes life a lot easier.
void* 会起作用。您只需要进行一次强有力的
reinterpret_cast
即可。但我不会使用这个解决方案。 Boost 有很多创建函子对象的方法: http:// www.boost.org/doc/libs/1_46_1/doc/html/function.htmlA void* would work. You just have to do a strong
reinterpret_cast
. But I would not use this solution. Boost has a bunch of ways of creating functor objects: http://www.boost.org/doc/libs/1_46_1/doc/html/function.html