he code below is the public interface of the ThreadPool class.
The lines 7-10 define a light copy-by-value semantics for the class. The
instances of the class are intended for local scope usage and
passing-by-value. Each local copy points to the same structure.
The executed tasks are supplied to the ThreadPool as
boost::function0<void> objects. Such policy is defined by lines 4 and
12. The user must supply exactly the number of Jobs specified in the
constructor before calling the run() function.
The onRestart function, line 13, identifies the operation that would be
executed when some working thread is restarted.
The exit() function, line 14, commands the ThreadPool to exit next time the
executed tasks reach completion or throw.
The run() function, line 15, starts the execution. After the run() is called,
the ThreadPool becomes immutable.
The execution would continue even if all ThreadPool objects go out of scope.
1\ class ThreadPool
2\ {
3\ public:
4\
typedef
boost::function0<void> Job;
6\ public:
7\
ThreadPool();
8\
explicit
ThreadPool( int nThreads );
9\
ThreadPool(
const ThreadPool& );
10\
ThreadPool&
operator=( const ThreadPool& );
11\ public:
12\
void
add( const Job& job );
13\
void
onRestart( const Job& job );
14\
void
exit();
15\
void
run();
16\
bool
isRunning() const;
17\
class
AlreadyRunningException;
18\
class
TooManyJobs;
19\
class
WrongNumberOfJobs;
20\ };
|