I am trying to create a threadpool that can run functions from unknown classes. I do not want to create non-members as a proxy. I managed to create a working class and a workthread class and a task structure, all of which are templates.
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) {
...
}
bool doTask() {
Task<T> task;
...
if(task.func && task.inst) {
(task)();
}
}
private:
std::queue<Task<T>> mTasks;
};
As with this code, I define a class for ThreadPool and Task. But I want to be able to name members of unknown class types. I was considering void ptr, but I could not find a way to convert this to a valid instance of ptr. I also looked at boost :: mem_fun, but struggled to handle it.
I read briefly about C ++ 0x and, as I understand it, it should make it easier to solve my problem, but I would like to solve it before that, if at all possible.