C++ POCO - 如何在线程内从线程池启动线程

问题描述 投票:0回答:0

我正在使用 POCO 默认线程池来启动一个事件接收器线程来侦听事件。收到事件后,我需要在单独的线程中处理该事件。

这是我正在尝试做的事情,但我无法在新线程中启动事件处理器。当我尝试这样做时,应用程序挂起。我尝试事件打印线程池统计信息,但应用程序在我调用默认线程池时挂起。

    #include "Poco/ThreadPool.h"
    #include "Poco/Runnable.h"
    #include <iostream>
    #include <thread>

    class EventReceiver : public Poco::Runnable
    {
    public:
        void run()
        {    
            // App hangs when i try to do this
            std::cout  << "Capacity: " <<   Poco::ThreadPool::defaultPool().capacity() << endl;
            
           Poco::ThreadPool::defaultPool().start(eventProcessor);
        }
    };

    class EventProcessor : public Poco::Runnable
    {
    public:
        void run()
        {
           // Event shall be processed here 
        }      
    }; 

    int main(int argc, char** argv)
    {
        EventReceiver eventReceiver;
        Poco::ThreadPool::defaultPool().start(eventReceiver);
        Poco::ThreadPool::defaultPool().joinAll();
        return 0;
    }
c++ poco-libraries
© www.soinside.com 2019 - 2024. All rights reserved.