POCO委托并行执行

问题描述

我们已经实现了多线程应用程序,我们从外部世界获得了多个请求。我们使用POCO基础框架使用poco基本事件将每个请求委托给新线程 执行如下-

class msgHandler
{
   public:
   void processEvent(const void* param1,const string & param2)
   {
      cout<<"Processing Event"<<endl;
      for(int i =0 ; i < 3; i++)
      {
         cout<<pthread_self()<<":"<<i<<endl;
         sleep(1);
      }
      cout<<"Processing event completed"<<endl;
   }
};

int main()
{
   Poco::BasicEvent<const string> theEvent;
   msgHandler *handlerPtr = new msgHandler;
   string str = "Test";

   theEvent += Poco::Delegate<msgHandler,const string>(handlerPtr,&msgHandler::processEvent);

   cout<<"First Call"<<endl;
   theEvent.notifyAsync(handlerPtr,str);

   cout<<"Second Call"<<endl;
   theEvent.notifyAsync(handlerPtr,str);   

   cout<<"Third Call"<<endl;
   theEvent.notifyAsync(handlerPtr,str);

   cout<<"Fourth Call"<<endl;
   theEvent.notifyAsync(handlerPtr,str);

   while(1);
   return 0;   }


   Output
   First Call
   Second Call
   Third Call
   Processing Event
   140200064788224:0
   Fourth Call
   140200064788224:1
   140200064788224:2
   Processing event completed
   Processing Event
   140200056395520:0
   140200056395520:1
   140200056395520:2
   Processing event completed
   Processing Event
   140200048002816:0
   140200048002816:1
   140200048002816:2
   Processing event completed
   Processing Event
   140200039610112:0
   140200039610112:1
   140200039610112:2
   Processing event completed

已观察到:所有线程的顺序执行意味着仅在执行“首次呼叫”线程,“第二次呼叫”线程之后才进行调度

期望:NotifyAsync应该产生单独的线程,并且所有线程应该并行运行。意味着“首次呼叫”,“第二次呼叫”和“第三次呼叫”线程应并行运行。

根据发行说明,Poco版本 欣赏是否可以发布代码段。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)