ToolDAQFramework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
MyToolMultiThread.cpp
Go to the documentation of this file.
1 #include "MyToolMultiThread.h"
2 
4 
6 
7 
9 
10 
11 bool MyToolMultiThread::Initialise(std::string configfile, DataModel &data){
12 
13  if(configfile!="") m_variables.Initialise(configfile);
14  //m_variables.Print();
15 
16  m_data= &data;
17  m_log= m_data->Log;
18 
19  if(!m_variables.Get("verbose",m_verbose)) m_verbose=1;
20 
21  int threadcount=0;
22  if(!m_variables.Get("Threads",threadcount)) threadcount=4;
23 
24  m_util=new Utilities(m_data->context);
25 
26  for(int i=0;i<threadcount;i++){
28  tmparg->busy=0;
29  tmparg->message="";
30  args.push_back(tmparg);
31  std::stringstream tmp;
32  tmp<<"T"<<i;
33  m_util->CreateThread(tmp.str(), &Thread, args.at(i));
34  }
35 
36  m_freethreads=threadcount;
37 
38 
39 
40  return true;
41 }
42 
43 
45 
46  for(int i=0; i<args.size(); i++){
47  if(args.at(i)->busy==0){
48  std::cout<<"reply="<<args.at(i)->message<<std::endl;
49  args.at(i)->message="Hi";
50  args.at(i)->busy=1;
51  break;
52  }
53 
54  }
55 
56  m_freethreads=0;
57  for(int i=0; i<args.size(); i++){
58  if(args.at(i)->busy==0) m_freethreads++;
59  }
60 
61  std::cout<<"free threads="<<m_freethreads<<":"<<args.size()<<std::endl;
62 
63  sleep(1);
64 
65  return true;
66 }
67 
68 
70 
71  for(int i=0;i<args.size();i++) m_util->KillThread(args.at(i));
72 
73  args.clear();
74 
75  delete m_util;
76  m_util=0;
77 
78  return true;
79 }
80 
82 
83  MyToolMultiThread_args* args=reinterpret_cast<MyToolMultiThread_args*>(arg);
84 
85  if(!args->busy) usleep(100);
86  else{
87 
88  args->message="Hello";
89  sleep(10);
90 
91  args->busy=0;
92  }
93 
94 }
static void Thread(Thread_args *arg)
Function to be run by the thread in a loop. Make sure not to block in it.
Logging * Log
Log class pointer for use in Tools, it can be used to send messages which can have multiple error lev...
Definition: DataModel.h:60
Thread_args * CreateThread(std::string ThreadName, void(*func)(std::string))
Definition: Utilities.cpp:130
int m_freethreads
Keeps track of free threads.
std::vector< MyToolMultiThread_args * > args
Vector of thread args (also holds pointers to the threads)
MyToolMultiThread()
Simple constructor.
bool Finalise()
Finalise funciton used to clean up resorces.
bool KillThread(Thread_args *&args)
Kill a thread assosiated to args.
Definition: Utilities.cpp:234
bool Initialise(std::string configfile, DataModel &data)
Initialise Function for setting up Tool resorces.
Utilities * m_util
Pointer to utilities class to help with threading.
bool Execute()
Executre function used to perform Tool perpose.