ToolDAQFramework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
MyToolDynamicMultiThread.cpp
Go to the documentation of this file.
2 
4 
6 
7 
9 
10 
11 bool MyToolDynamicMultiThread::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  m_util=new Utilities(m_data->context);
22 
23  m_threadnum=0;
24  CreateThread();
25 
26  m_freethreads=1;
27 
28 
29 
30  return true;
31 }
32 
33 
35 
36  for(int i=0; i<args.size(); i++){
37  if(args.at(i)->busy==0){
38  std::cout<<"reply="<<args.at(i)->message<<std::endl;
39  args.at(i)->message="Hi";
40  args.at(i)->busy=1;
41  break;
42  }
43 
44  }
45 
46  m_freethreads=0;
47  int lastfree=0;
48  for(int i=0; i<args.size(); i++){
49  if(args.at(i)->busy==0){
50  m_freethreads++;
51  lastfree=i;
52  }
53  }
54 
56  if(m_freethreads>1) DeleteThread(lastfree);
57 
58  std::cout<<"free threads="<<m_freethreads<<":"<<args.size()<<std::endl;
59 
60  sleep(1);
61 
62  return true;
63 }
64 
65 
67 
68  for(int i=0;i<args.size();i++) m_util->KillThread(args.at(i));
69 
70  args.clear();
71 
72  delete m_util;
73  m_util=0;
74 
75  return true;
76 }
77 
79 
81  tmparg->busy=0;
82  tmparg->message="";
83  args.push_back(tmparg);
84  std::stringstream tmp;
85  tmp<<"T"<<m_threadnum;
86  m_util->CreateThread(tmp.str(), &Thread, args.at(args.size()-1));
87  m_threadnum++;
88 
89 }
90 
92 
93  m_util->KillThread(args.at(pos));
94  delete args.at(pos);
95  args.at(pos)=0;
96  args.erase(args.begin()+(pos-1));
97 
98  }
99 
101 
103 
104  if(!args->busy) usleep(100);
105  else{
106 
107  args->message="Hello";
108  sleep(10);
109 
110  args->busy=0;
111  }
112 
113 }
bool Execute()
Executre function used to perform Tool perpose.
unsigned long m_threadnum
Counter for unique naming of threads.
void CreateThread()
Function to Create Thread.
bool Finalise()
Finalise funciton used to clean up resorces.
void DeleteThread(int pos)
Function to delete thread.
Utilities * m_util
Pointer to utilities class to help with threading.
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
MyToolDynamicMultiThread()
Simple constructor.
static void Thread(Thread_args *arg)
Function to be run by the thread in a loop. Make sure not to block in it.
bool KillThread(Thread_args *&args)
Kill a thread assosiated to args.
Definition: Utilities.cpp:234
int m_freethreads
Keeps track of free threads.
bool Initialise(std::string configfile, DataModel &data)
Initialise Function for setting up Tool resorces.
std::vector< MyToolDynamicMultiThread_args * > args
Vector of thread args (also holds pointers to the threads)