C ++ POCO - Как запустить поток в пуле потоков без использования метода run ()? - PullRequest
0 голосов
/ 13 сентября 2018

То, как библиотеки C ++ POCO документация показывает, как запустить thread с использованием thread-pool, выглядит следующим образом:

#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 и запустить другой thread (используя тот же thread-pool) из другого метода, кроме виртуального метода run().

Возможно ли это в библиотеках 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;
    }

1 Ответ

0 голосов
/ 13 сентября 2018

Я рад поделиться ответом.

Решение использует класс 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;
}
...