BatchQueue.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2007 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/Util/genutil/BatchQueue.h
10 
11 #ifndef __BatchQueue_h
12 #define __BatchQueue_h
13 
14 #include "ThreadContext.h"
15 #include "Functor.h"
16 
17 #include <vector>
18 
20 class BatchQueue {
21 
22 public:
23 
25  BatchQueue (unsigned nthread = 0);
26 
28  ~BatchQueue ();
29 
31  void resize (unsigned nthread);
32 
34  class Job;
35 
37  void submit (Job*);
38 
40  template<class Class, typename Method>
41  void submit (Class* instance, Method method);
42 
44  template<class Class, typename Method, typename Argument>
45  void submit (Class* instance, Method method, Argument argument);
46 
48  template<class Class, typename Method, typename Arg1, typename Arg2>
49  void submit (Class* instance, Method method, Arg1 a1, Arg2 a2);
50 
52  void wait ();
53 
54 protected:
55 
57  void add (Job*);
58 
60  void remove (Job*);
61 
64 
66  std::vector<Job*> active;
67 
68 };
69 
70 class BatchQueue::Job {
71 
72 public:
73 
74  virtual ~Job () {}
75 
77  void run ();
78 
80  virtual void execute () = 0;
81 
82 protected:
83 
84  friend class BatchQueue;
85 
87  BatchQueue* queue;
88 
89 };
90 
91 template<class Class, typename Method>
92 void BatchQueue::submit (Class* instance, Method method)
93 {
94  class Job0 : public Job {
95  Functor< void() > functor;
96  void execute () { functor (); }
97  public:
98  Job0 (Class* instance, Method method) : functor (instance, method) {}
99  };
100 
101  submit (new Job0( instance, method ));
102 }
103 
104 template<class Class, typename Method, typename Argument>
105 void BatchQueue::submit (Class* instance, Method method, Argument arg)
106 {
107  class Job1 : public Job {
108  Functor< void(Argument) > functor;
109  Argument argument;
110  void execute () { functor (argument); }
111  public:
112  Job1 (Class* instance, Method method, Argument arg)
113  : functor (instance, method) { argument = arg; }
114  };
115 
116  submit (new Job1( instance, method, arg ));
117 }
118 
119 template<class Class, typename Method, typename Arg1, typename Arg2>
120 void BatchQueue::submit (Class* instance, Method method, Arg1 a1, Arg2 a2)
121 {
122  class Job2 : public Job {
123  Functor< void(Arg1, Arg2) > functor;
124  Arg1 arg1;
125  Arg2 arg2;
126  void execute () { functor (arg1, arg2); }
127  public:
128  Job2 (Class* instance, Method method, Arg1 a1, Arg2 a2)
129  : functor (instance, method) { arg1 = a1; arg2 = a2; }
130  };
131 
132  submit (new Job2( instance, method, a1, a2 ));
133 }
134 
135 #endif // !defined(__BatchQueue_h)
std::vector< Job * > active
The active jobs.
Definition: BatchQueue.h:71
void add(Job *)
Add a job to the active list.
void remove(Job *)
Remove a job from the active list.
Locks the mutex on construction and unlocks on deletion.
Definition: ThreadContext.h:56
A convenient exception handling class.
Definition: Error.h:54
void submit(Job *)
Submit a job for processing.
Definition: BatchQueue.C:218
void resize(unsigned nthread)
Set the number of tasks that may run at one time.
Definition: BatchQueue.C:227
BatchQueue(unsigned nthread=0)
Default constructor.
Definition: BatchQueue.C:20
ThreadContext * context
Mutual exclusion and condition variables used to coordinate the queue.
Definition: BatchQueue.h:68
void wait()
Wait for completion of all active jobs.
Definition: BatchQueue.C:223
Implements an adaptable function object in compliance with the STL.
Definition: Functor.h:39
ThreadContext class.
Definition: ThreadContext.h:15
const std::string get_message() const
Get the error message.
Definition: Error.C:133
~BatchQueue()
Destructor.
Definition: BatchQueue.C:33
unsigned nthread
Number of threads that will use the plan.
Definition: FTransform.C:19
Runs multiple threads using a simple batch queue model.
Definition: BatchQueue.h:20

Generated using doxygen 1.8.17