tjthread_code.h
1 #include <tjutils/tjthread.h>
2 #include <tjutils/tjlog.h>
3 
4 
5 template<typename In, typename Out, typename Local>
7  Log<ThreadComponent> odinlog("WorkThread","run");
8  while(1) {
9 
10  process.wait();
11  process.reset(); // Reset for next event
12  ODINLOG(odinlog,normalDebug) << "catched process signal, cont=" << tloop->cont << STD_endl;
13  if(!tloop->cont) break;
14 
15  ODINLOG(odinlog,normalDebug) << "processing thread " << begin << "/" << end << STD_endl;
16 
17  status=tloop->kernel(*tloop->in_cache, *out_cache, local, begin, end);
18 
19  ODINLOG(odinlog,normalDebug) << "signaling finished=" << status << STD_endl;
20  finished.signal();
21 
22  if(!status) break;
23  }
24 }
25 
27 
28 
29 template<typename In, typename Out, typename Local>
30 bool ThreadedLoop<In,Out,Local>::init(unsigned int numof_threads, unsigned int loopsize) {
31  Log<ThreadComponent> odinlog("ThreadedLoop","init");
32  mainbegin=0;
33  mainend=loopsize;
34 #ifndef NO_THREADS
35  destroy(); // stop old threads
36  ODINLOG(odinlog,normalDebug) << "numof_threads=" << numof_threads << STD_endl;
37  if(numof_threads>1) {
38  threads.resize(numof_threads-1); // the main thread is also used
39  unsigned int onesize=loopsize/numof_threads;
40  unsigned int rest=loopsize%numof_threads;
41  unsigned int count=0;
42  for(unsigned int i=0; i<(numof_threads-1); i++) {
43  threads[i]=new WorkThread(this);
44  threads[i]->begin=count;
45  count+=onesize;
46  if(i<rest) count++;
47  threads[i]->end=count;
48  threads[i]->start();
49  }
50  mainbegin=count;
51  count+=onesize;
52  if((numof_threads-1)<rest) count++;
53  mainend=count;
54  }
55 #endif
56  return true;
57 }
58 
59 template<typename In, typename Out, typename Local>
61  Log<ThreadComponent> odinlog("ThreadedLoop","destroy");
62 #ifndef NO_THREADS
63  cont=false; // Stop threads
64  for(unsigned int i=0; i<threads.size(); i++) {
65  threads[i]->process.signal();
66  threads[i]->wait();
67  delete threads[i];
68  }
69  threads.resize(0);
70 #endif
71  }
72 
73 template<typename In, typename Out, typename Local>
74 bool ThreadedLoop<In,Out,Local>::execute(const In& in, STD_vector<Out>& outvec) {
75  Log<ThreadComponent> odinlog("ThreadedLoop","execute");
76 #ifdef NO_THREADS
77  outvec.resize(1);
78  return kernel(in, outvec[0], mainlocal, mainbegin, mainend);
79 #else
80 
81  unsigned int nthreads=threads.size();
82 
83  outvec.resize(nthreads+1);
84 
85  if(nthreads) {
86  in_cache=&in;
87  cont=true;
88 
89  for(unsigned int i=0; i<nthreads; i++) {
90  threads[i]->out_cache=&(outvec[i]);
91  threads[i]->status=true;
92  threads[i]->process.signal();
93  }
94  }
95 
96  bool result=kernel(in, outvec[nthreads], mainlocal, mainbegin, mainend); // Use mainthread also for kernel
97 
98  if(nthreads) {
99  for(unsigned int i=0; i<nthreads; i++) {
100  threads[i]->finished.wait(); // Wait for result
101  threads[i]->finished.reset();
102  if(!threads[i]->status) result=false;
103  }
104  ODINLOG(odinlog,normalDebug) << "finished.wait() done" << STD_endl;
105  }
106 
107  return result;
108 #endif
109 }
110 
111 
bool init(unsigned int numof_threads, unsigned int loopsize)
Definition: tjthread_code.h:30
Definition: tjlog.h:218
bool execute(const In &in, STD_vector< Out > &outvec)
Definition: tjthread_code.h:74
void destroy()
Definition: tjthread_code.h:60