所以我的问题实际上有几个部分:
使用 Poco 线程库:
- 将数据传递给线程的所有可能方法是什么(在线程调用和已运行的线程中)。
- 您更喜欢哪些方法,为什么?您能否提供有关您使用这些方法的体验的任何其他信息?
- Applied Informatics(Poco 的作者)推荐了哪些方法? Applied Informatics 是否提供了概述向线程传递参数的任何其他文档?
我已经看过这里了:
- http://pocoproject.org/docs/package-Foundation.Threading-index.html
- http://pocoproject.org/slides/130-Threads.pdf
提前致谢...
最佳答案
将参数传递给新线程的规范方法是通过您需要创建为线程入口点的 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
关于c++ - 使用 Poco C++ 库,如何将数据传递给线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11659436/