Sophie

Sophie

distrib > Fedora > 18 > x86_64 > by-pkgid > 8c86774a3e53d77cc119f53a2b94a57a > files > 1333

root-tutorial-5.34.14-2.fc18.noarch.rpm

// Usage:
// root [0] .L threadPool.C++
// root [1] threadPool(10)  10 = numThreads

// STD
#include <iostream>
#include <iterator>
#include <vector>
#include <unistd.h>
// ThreadPool
#include "TThreadPool.h"
// ROOT
#include "TThread.h"

//=============================================================================
using namespace std;
//=============================================================================
const size_t g_sleeptime = 1; // in secs.
const size_t g_multTasks = 50;
//=============================================================================

// define a custom parameters type for task objects
enum EProc {start, clean};

// a class defining task objects
class TTestTask: public TThreadPoolTaskImp<TTestTask, EProc>
{
public:
   bool runTask(EProc /*_param*/) {
      m_tid = TThread::SelfId();
      sleep(g_sleeptime);
      return true;
   }
   unsigned long threadID() const {
      return m_tid;
   }

private:
   unsigned long m_tid;
};

//=============================================================================
void threadPool(size_t _numThreads, bool _needDbg = false)
{
   cout << "ThreadPool: starting..." << endl;
   // number of tasks to process
   size_t numTasks(_numThreads * g_multTasks);

   // create a thread pool object
   // _numThreads - a number of threads in the pool
   // _needDbg - defines whether to show debug messages  
   TThreadPool<TTestTask, EProc> threadPool(_numThreads, _needDbg);

   // create a container of tasks
   vector <TTestTask> tasksList(numTasks);

   cout << "ThreadPool: getting tasks..." << endl;
   cout << "ThreadPool: processing tasks..." << endl;
   // push tasks to the ThreadPool
   // tasks can be also pushed asynchronously
   for (size_t i = 0; i < numTasks; ++i) {
      threadPool.PushTask(tasksList[i], start);
   }

   // Stop thread pool.
   // The parameter "true" requests the calling thread to wait,
   // until the thread pool task queue is drained.
   threadPool.Stop(true);
   cout << "ThreadPool: done" << endl;
}