如何解决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 举报,一经查实,本站将立刻删除。