mardi 8 septembre 2020

POCO Delegate Parallel Execution

We have implemented multi threaded application where we get multiple requests from the external world. We have used POCO foundation framework to delegate every request to new thread using poco basic events for execution as below -

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

Observed : Sequential execution of all the threads means only after execution of "First Call" thread, "Second Call" thread is getting scheduled

Expectation : NotifyAsync should spawn separate thread and all threads should run in parallel. Means "First Call", "Second Call" and "Third Call" threads should run in parallel.

As per release notes, Poco version < 1.4 had this behavior, later they changed it. Can anyone help on how to achieve parallel execution of threads using newer version of poco delegate.
Appreciate if code snippet can be posted.

Aucun commentaire:

Enregistrer un commentaire