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

发布于 2024-11-09 11:54:19 字数 1302 浏览 6 评论 0原文

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

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风向决定发型 2024-11-16 11:54:19

为什么要使用 T*,而不是仅仅使用 boost::function

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

类 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 结合使用,当函数超出范围时(如果它是最后一个引用),您可以自动销毁对象。这让生活变得更加轻松。

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:

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

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:

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));
}

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.

橙幽之幻 2024-11-16 11:54:19

void* 会起作用。您只需要进行一次强有力的reinterpret_cast即可。但我不会使用这个解决方案。 Boost 有很多创建函子对象的方法: http:// www.boost.org/doc/libs/1_46_1/doc/html/function.html

A 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文