2012-07-25 87 views
6

所以我的问题实际上有几个部分组成:使用Poco C++库,我如何将数据传递给线程?

使用波苏线程库:

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

我在这里已经看:提前

谢谢...传递参数到一个新的线程

回答

15

的正规途径是通过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

+2

从人自己。 Günter是Applied Informatics的创始人和Poco C++ Library的项目负责人。非常感谢Günter花时间回答这个问题! – Homer6 2012-07-26 00:32:28

+0

对于遇到这篇文章的人来说,我也发现这个链接很有用:http://www.cs.bgu.ac.il/~spl111/PracticalSession09_-_Threads_Using_POCO – Homer6 2012-07-26 00:46:44