3

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

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

4

2 回答 2

3

为什么要使用 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 结合使用,当函数超出范围(如果它是最后一个引用)时,您将自动销毁对象。这让生活变得轻松了很多。

于 2011-05-23T12:10:42.853 回答
0

一个 void* 会起作用。你只需要做一个强者reinterpret_cast。但我不会使用这个解决方案。Boost有很多创建仿函数对象的方法:http: //www.boost.org/doc/libs/1_46_1/doc/html/function.html

于 2011-05-23T12:04:13.777 回答