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
21
22public:
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
54protected:
55
57 void add (Job*);
58
60 void remove (Job*);
61
64
66 std::vector<Job*> active;
67
68};
69
70class BatchQueue::Job {
71
72public:
73
74 virtual ~Job () {}
75
77 void run ();
78
80 virtual void execute () = 0;
81
82protected:
83
84 friend class BatchQueue;
85
87 BatchQueue* queue;
88
89};
90
91template<class Class, typename Method>
92void 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
104template<class Class, typename Method, typename Argument>
105void 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
119template<class Class, typename Method, typename Arg1, typename Arg2>
120void 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)
ThreadContext * context
Mutual exclusion and condition variables used to coordinate the queue.
Definition BatchQueue.h:63
void wait()
Wait for completion of all active jobs.
Definition BatchQueue.C:237
~BatchQueue()
Destructor.
Definition BatchQueue.C:36
void submit(Job *)
Submit a job for processing.
Definition BatchQueue.C:232
std::vector< Job * > active
The active jobs.
Definition BatchQueue.h:66
void resize(unsigned nthread)
Set the number of tasks that may run at one time.
Definition BatchQueue.C:241
BatchQueue(unsigned nthread=0)
Default constructor.
Definition BatchQueue.C:23
void remove(Job *)
Remove a job from the active list.
void add(Job *)
Add a job to the active list.
Implements an adaptable function object in compliance with the STL.
Definition Functor.h:39
ThreadContext class.
Definition ThreadContext.h:15

Generated using doxygen 1.14.0