【问题标题】:Using the Poco C++ Library, how can I pass data to a thread?使用 Poco C++ 库,如何将数据传递给线程?
【发布时间】:2012-07-24 10:58:51
【问题描述】:

所以我的问题实际上有几个部分:

使用 Poco 线程库:

  1. 将数据传递给线程的所有可能方法是什么(在线程调用和已经运行的线程中)。
  2. 您更喜欢哪些方法,为什么?您能否提供有关您使用这些方法的经验的任何其他信息?
  3. Applied Informatics(Poco 的作者)推荐了哪些方法? Applied Informatics 是否提供了任何其他文档来概述将参数传递给线程?

我已经看过这里了:

提前谢谢...

【问题讨论】:

    标签: c++ multithreading argument-passing poco-libraries


    【解决方案1】:

    将参数传递给新线程的规范方式是通过您需要创建为线程入口点的 Runnable 子类。示例:

    class MyThread: public Poco::Runnable
    {
    public:
        MyThread(const std::string& arg1, int arg2):
            _arg1(arg1),
            _arg2(arg2)
        {
        }
    
        void run()
        {
            // use _arg1 and _arg2;
            //...
        }
    
    private:
        std::string _arg1;
        int _arg2;
    };
    
    //...
    
    MyThread myThread("foo", 42);
    Poco::Thread thread;
    thread.start(myThread);
    thread.join();
    

    对于将数据传递给已经运行的线程,最佳解决方案是什么取决于您的要求。对于典型的工作线程场景,请考虑使用Poco::NotificationQueue。可以在此处找到带有解释的完整示例:http://pocoproject.org/slides/090-NotificationsEvents.pdf

    【讨论】:

    猜你喜欢
    • 2014-12-24
    • 2016-11-23
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2023-04-02
    相关资源
    最近更新 更多