gpt4 book ai didi

c++ - 存储并稍后调用未知类的成员函数

转载 作者:太空狗 更新时间:2023-10-29 21:28:53 25 4
gpt4 key购买 nike

我正在尝试创建一个可以运行来自未知类的函数的线程池。我不希望必须创建非成员作为代理。我设法创建了一个工作池和工作线程类以及一个任务结构,所有这些都是模板。

// 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 的内容,据我了解,它应该可以使我的问题更容易解决,但如果可能的话,我想在此之前解决这个问题。

最佳答案

为什么要使用 T*,而不仅仅是 boost::function<void ()>

这样您就可以使用自由函数和成员函数,并且可以简化您的代码。

类 X 实例上成员的任务可以像这样排队:

poll.add(boost::bind(&X::member, x_instance, other_arguments));

代码中没有转换和模板。

更新:

使用 boost::function 而不是您的 Task 类。然后您只需要跟踪实例并适本地调用它们。例如:

class TaskQueue {
std::deque<boost::function<void ()> > m_tasks;

public:
void add(boost::function<void ()> const& f) { m_tasks.push_back(f); }
bool has_task() const { return !m_tasks.empty(); }
void do_task() {
m_tasks.front()();
m_tasks.pop_front();
}
};

int example_enqueue(TaskQueue* tq) {
boost::shared_ptr<RandomClass> rc(new RandomClass);
tq->add(boost::bind(&RandomClass::method, rc, arg_1, arg_whatever));
}

请注意,通过将此方法与 boost::shared_ptr 结合使用,当函数超出范围时,如果它是最后一个引用,您将自动销毁对象。这让生活变得更加轻松。

关于c++ - 存储并稍后调用未知类的成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6096860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com