Overview
Features
Download
Documentation
Community
Add-Ons & Services

Question on TaskManager usage

Please post support and help requests here.

Question on TaskManager usage

Postby mushubi » 08 Mar 2007, 21:26

Hi
What is the correct usage of TaskManager?
I want to start several tasks ( i..e to have these tasks run conrurrently ) and I am doing it this way ( only main() of my Application type prgram is shown)

Code: Select all

   int main(const std::vector& args)
   {   
      TaskManager tm;
      tm.start(new Task1("Task1 "));
      tm.start(new Task1("Task2 "));
      tm.start(new Task1("Task3 "));
      tm.joinAll();
      return Application::EXIT_OK;
   }

But only "Task 1" gets run. Even with "Task 1" its method get run multiple times which is not what I want. Is Task manager the correct approach if I want to run several long running tasks concurrently?

Thanks.
mushubi
 
Posts: 4
Joined: 07 Nov 2006, 05:18

Re: Question on TaskManager usage

Postby guenter » 09 Mar 2007, 19:23

Your code is basically right. The run method of your task will only be called once by the task manager. Maybe you could share the rest of your program, so that it's easier for me to help you. Below is a modified version of the SampleServer sample (Util) that uses multiple tasks. Maybe this will help you.

Code: Select all

#include "Poco/Util/ServerApplication.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Task.h"
#include "Poco/TaskManager.h"
#include "Poco/DateTimeFormatter.h"
#include


using Poco::Util::Application;
using Poco::Util::ServerApplication;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::OptionCallback;
using Poco::Util::HelpFormatter;
using Poco::Task;
using Poco::TaskManager;
using Poco::DateTimeFormatter;


class SampleTask: public Task
{
public:
   SampleTask(const std::string& name): Task(name)
   {
   }
   
   void runTask()
   {
      Application& app = Application::instance();
      while (!sleep(1000))
      {
         Application::instance().logger().information(name() + ": busy doing nothing... " + DateTimeFormatter::format(app.uptime()));
      }
   }
};


class SampleServer: public ServerApplication
{
public:
   SampleServer(): _helpRequested(false)
   {
   }
   
   ~SampleServer()
   {
   }

protected:
   void initialize(Application& self)
   {
      loadConfiguration(); // load default configuration files, if present
      ServerApplication::initialize(self);
      logger().information("starting up");
   }
      
   void uninitialize()
   {
      logger().information("shutting down");
      ServerApplication::uninitialize();
   }

   void defineOptions(OptionSet& options)
   {
      ServerApplication::defineOptions(options);
      
      options.addOption(
         Option("help", "h", "display help information on command line arguments")
            .required(false)
            .repeatable(false)
            .callback(OptionCallback(this, &SampleServer::handleHelp)));
   }

   void handleHelp(const std::string& name, const std::string& value)
   {
      _helpRequested = true;
      displayHelp();
      stopOptionsProcessing();
   }

   void displayHelp()
   {
      HelpFormatter helpFormatter(options());
      helpFormatter.setCommand(commandName());
      helpFormatter.setUsage("OPTIONS");
      helpFormatter.setHeader("A sample server application that demonstrates some of the features of the Util::ServerApplication class.");
      helpFormatter.format(std::cout);
   }

   int main(const std::vector& args)
   {
      if (!_helpRequested)
      {
         TaskManager tm;
         tm.start(new SampleTask("Task 1"));
         tm.start(new SampleTask("Task 2"));
         tm.start(new SampleTask("Task 3"));
         waitForTerminationRequest();
         tm.cancelAll();
         tm.joinAll();
      }
      return Application::EXIT_OK;
   }
   
private:
   bool _helpRequested;
};


POCO_SERVER_MAIN(SampleServer)
guenter
 
Posts: 1091
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to Support

Who is online

Users browsing this forum: No registered users and 2 guests