C++ POCO - 如何在不使用 run() 方法的情况下启动线程池上的线程?
C++ POCO - How to launch a thread on a thread-pool without using the run() method?
C++ POCO 库文档演示如何使用thread-pool
启动thread
的方式如下所示:
#include "Poco/ThreadPool.h"
#include "Poco/Runnable.h"
#include <iostream>
#include <thread>
class Foo : public Poco::Runnable
{
public:
virtual void run()
{
while (true)
{
std::cout << "Hello Foo " << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
};
int main(int argc, char** argv)
{
Foo foo;
Poco::ThreadPool::defaultPool().addCapacity(16);
Poco::ThreadPool::defaultPool().startWithPriority(Poco::Thread::Priority::PRIO_LOW, foo);
Poco::ThreadPool::defaultPool().joinAll();
return 0;
}
这完全可以正常工作。
但是,现在我想使用相同的class Foo
,并从虚拟方法run()
之外的另一种方法启动另一个thread
(使用相同的thread-pool
(。
这在 POCO 图书馆上可能吗?如果是这样,我该怎么做?
像这样的伪代码:
#include "Poco/ThreadPool.h"
#include "Poco/Runnable.h"
#include <iostream>
#include <thread>
class Foo : public Poco::Runnable
{
public:
virtual void run()
{
// ERROR: How can I launch this thread on the method anotherThread()?
Poco::ThreadPool::defaultPool().startWithPriority(Poco::Thread::Priority::PRIO_LOW, anotherThread);
while (true)
{
std::cout << "Hello Foo " << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
void anotherThread() // This is the new thread!!
{
while (true)
{
std::cout << "Hello anotherThread " << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
};
int main(int argc, char** argv)
{
Foo foo;
Poco::ThreadPool::defaultPool().addCapacity(16);
Poco::ThreadPool::defaultPool().startWithPriority(Poco::Thread::Priority::PRIO_LOW, foo);
Poco::ThreadPool::defaultPool().joinAll();
return 0;
}
我很高兴分享答案。
解决方案是使用Poco::RunnableAdapter
类。
这是以防其他人发现相同的问题:
#include <Poco/Runnable.h>
#include <Poco/Thread.h>
#include <Poco/ThreadPool.h>
#include <Poco/ThreadTarget.h>
#include <Poco/RunnableAdapter.h>
#include <string>
#include <iostream>
class Foo : public Poco::Runnable
{
public:
virtual void run()
{
std::cout << " run() start" << std::endl;
Poco::Thread::sleep(200);
std::cout << " run() end" << std::endl;
}
void anotherThread()
{
std::cout << " anotherThread() start" << std::endl;
Poco::Thread::sleep(200);
std::cout << " anotherThread() end" << std::endl;
}
};
int main()
{
Poco::ThreadPool::defaultPool().addCapacity(16);
Foo foo;
Poco::RunnableAdapter<Foo> bar(foo, &Foo::anotherThread);
Poco::ThreadPool::defaultPool().startWithPriority(Poco::Thread::Priority::PRIO_LOW, foo);
Poco::ThreadPool::defaultPool().startWithPriority(Poco::Thread::Priority::PRIO_LOW, bar);
Poco::ThreadPool::defaultPool().joinAll();
return 0;
}
相关文章:
- 方法重写线程C++中的概念
- 使用基类中的派生方法运行线程,而无需使用模板
- 将线程中的数据存储到全局容器的最佳方法?
- 处理影响跨不同线程共享对象的定时回调的最佳方法是什么?
- 如何将带有参数的方法传递给线程以执行?
- C++ POCO - 如何在不使用 run() 方法的情况下启动线程池上的线程?
- 在销毁期间从另一个线程调用对象上调用方法是否未定义行为?
- C++中的多线程:连接线程的正确方法
- 使用包含互斥锁的类的方法实例化 cpp11 线程
- 如何在JNI中从线程内部调用JAVA方法
- 如何从另一个线程调用颤振引擎方法
- 在另一个线程上发出 QObject 信号的正确方法?
- 在C++线程内实现多个计时器的最安全方法
- C++ std::线程调用方法,从对象原因到调用此类的析构函数
- 将指针传递到另一个线程的正确方法
- 在另一个 QThread 上运行成员方法时,无法将事件发送到其他线程拥有的对象
- 支持多线程方法来构建数组中所有元素的集合吗
- 这两种线程方法之间有什么区别
- 什么是Qt中低延迟音频合成的最佳多线程方法
- 将不同的线程方法组合到Windows窗体应用程序中