【问题标题】:C++ POCO - How to launch a thread on a thread-pool without using the run() method?C++ POCO - 如何在不使用 run() 方法的情况下在线程池上启动线程?
【发布时间】:2018-09-13 09:10:41
【问题描述】:

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,并从虚拟方法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;
    }

【问题讨论】:

    标签: c++ multithreading threadpool poco-libraries


    【解决方案1】:

    我很高兴分享答案。

    解决方案是使用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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多