C++ POCO - 如何在不使用 run() 方法的情况下在线程池上启动线程?

标签 c++ multithreading threadpool poco-libraries

C++ POCO 库的方式 documentation展示了如何使用 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,并启动另一个 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;
    }

最佳答案

我很高兴分享答案。

解决方案是使用 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++ POCO - 如何在不使用 run() 方法的情况下在线程池上启动线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52310363/

相关文章:

c++ - curl 与 Visual Studio 2013

c++ - 128 位字符串数组使用 boost::spirit::*

android - 在android中设置录音的时间限制

java - 具有多线程的 REST Api,用于在 Spring Boot 中处理文件

c++ - OpenGL/C++ : Back to Front rendering with vertex buffers

.net - 为什么 Console.ReadKey() 会阻止在另一个线程中调用的 Console.WriteLine 的输出?

Java JVM 分析,线程状态 - "Monitor"状态是什么意思?

concurrency - 基础知识 |线程与响应式(Reactive)并发模型

java - 如何对不同的 ExecutorService 对象重用线程?

c++ - pion 网络库入门