C++;POCO-如何在不使用run()方法的情况下在线程池上启动线程? < C++的PoCo库显示如何使用线程池< /代码>启动线程,如下所示: #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; }
我很高兴与大家分享这个答案 解决方案是使用C++;POCO-如何在不使用run()方法的情况下在线程池上启动线程? < C++的PoCo库显示如何使用线程池< /代码>启动线程,如下所示: #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; },c++,multithreading,threadpool,poco-libraries,C++,Multithreading,Threadpool,Poco Libraries,我很高兴与大家分享这个答案 解决方案是使用Poco::RunnableAdapter类 这里是为了防止其他人发现相同的问题: #include <Poco/Runnable.h> #include <Poco/Thread.h> #include <Poco/ThreadPool.h> #include <Poco/ThreadTarget.h> #include <Poco/RunnableAdapter.h> #include &
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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
类Foo:publicpoco::Runnable
{
公众:
虚拟空运行()
{
标准::cout
#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;
}