The way the C++ POCO libraries documentation shows how to launch a thread using a thread-pool is like this:
#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;
}
Which works totally fine.
However, now I would like to use the same class Foo, and launch another thread (using the same thread-pool) from another method besides the virtual method run().
Is this possible on POCO libraries? If so, how can I do this?
Something like this pseudo-code:
#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;
}