简体   繁体   English

使用Poco C ++库,如何将数据传递给线程?

[英]Using the Poco C++ Library, how can I pass data to a thread?

So my question actually has several parts: 所以我的问题实际上有几个部分:

Using the Poco Threading Library: 使用Poco线程库:

  1. What are all of the possible methods for passing data to threads (at both thread invocation and for an already running thread). 将数据传递给线程的所有可能方法是什么(在线程调用和已经运行的线程中)。
  2. Which methods are preferred by you and why? 您最喜欢哪种方法?为什么? Can you provide any additional information about your experience using these methods? 您能否提供有关使用这些方法的经验的任何其他信息?
  3. Which methods are recommended by Applied Informatics (the author of Poco)? Applied Informatics(Poco的作者)推荐哪些方法? Is there any additional documentation supplied by Applied Informatics that outlines passing arguments to threads? Applied Informatics是否提供了其他文档,概述了将参数传递给线程?

I've looked here already: 我已经看过这里了:

Thanks in advance... 提前致谢...

The canonical way of passing arguments to a new thread is via the Runnable subclass you'll need to create as thread entry point. 将参数传递给新线程的规范方法是通过您需要创建的线程入口点的Runnable子类。 Example: 例:

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

For passing data to an already running thread, what's the best solution depends on your requirements. 要将数据传递给已经运行的线程,最佳解决方案取决于您的要求。 For a typical worker thread scenario, consider using Poco::NotificationQueue . 对于典型的工作线程场景,请考虑使用Poco :: NotificationQueue A complete sample with explanations can be found here: http://pocoproject.org/slides/090-NotificationsEvents.pdf 可以在此处找到包含解释的完整示例: http//pocoproject.org/slides/090-NotificationsEvents.pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM