我想創建一個可以運行未知類的函數的線程池。我不希望創建非會員作爲代理。 我設法創建了一個工作池& workerthread類和一個任務結構,所有這些都是模板。存儲和後來調用未知類的成員函數
// 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的類。但我希望能夠調用未知類類型的成員。我曾考慮過無效的ptr,但我找不到將其轉換爲有效實例ptr的方法。我也研究了boost :: mem_fun,但努力去真正去解決它。
我已經簡要地閱讀了關於C++ 0x的內容,並且從我的理解中,它應該能夠更輕鬆地解決我的問題,但如果可能的話,我想在此之前解決此問題。
看起來這可能是我的解決方案,但我如何將其作爲變量存儲以備後用? – Mattyspatty 2011-05-24 12:47:49
@Mattyspatty:查看更新的答案... – janm 2011-05-25 01:32:27
這真是太好了,謝謝! – Mattyspatty 2011-05-25 10:50:10