All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thread.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_THREAD_H_
36 #define _BLAZE_UTIL_THREAD_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <boost/scoped_ptr.hpp>
44 #include <boost/thread/thread.hpp>
45 #include <blaze/util/NonCopyable.h>
46 
47 
48 namespace blaze {
49 
50 //=================================================================================================
51 //
52 // ::blaze NAMESPACE FORWARD DECLARATIONS
53 //
54 //=================================================================================================
55 
56 class ThreadPool;
57 
58 
59 
60 
61 //=================================================================================================
62 //
63 // CLASS DEFINITION
64 //
65 //=================================================================================================
66 
67 //*************************************************************************************************
200 class Thread : private NonCopyable
201 {
202  private:
203  //**Type definitions****************************************************************************
204  typedef boost::scoped_ptr<boost::thread> ThreadHandle;
205  //**********************************************************************************************
206 
207  //**Constructors********************************************************************************
210  explicit Thread( ThreadPool* pool );
212  //**********************************************************************************************
213 
214  public:
215  //**Constructors********************************************************************************
218  template< typename Callable >
219  explicit inline Thread( Callable func );
220 
221  template< typename Callable, typename A1 >
222  explicit inline Thread( Callable func, A1 a1 );
223 
224  template< typename Callable, typename A1, typename A2 >
225  explicit inline Thread( Callable func, A1 a1, A2 a2 );
226 
227  template< typename Callable, typename A1, typename A2, typename A3 >
228  explicit inline Thread( Callable func, A1 a1, A2 a2, A3 a3 );
229 
230  template< typename Callable, typename A1, typename A2, typename A3, typename A4 >
231  explicit inline Thread( Callable func, A1 a1, A2 a2, A3 a3, A4 a4 );
232 
233  template< typename Callable, typename A1, typename A2, typename A3, typename A4, typename A5 >
234  explicit inline Thread( Callable func, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5 );
236  //**********************************************************************************************
237 
238  //**Destructor**********************************************************************************
241  ~Thread();
243  //**********************************************************************************************
244 
245  //**Utility functions***************************************************************************
248  inline bool joinable() const;
249  inline void join();
251  //**********************************************************************************************
252 
253  private:
254  //**Utility functions***************************************************************************
257  inline bool hasTerminated() const;
259  //**********************************************************************************************
260 
261  //**Thread execution functions******************************************************************
264  void run();
266  //**********************************************************************************************
267 
268  //**Member variables****************************************************************************
271  volatile bool terminated_;
272 
277 
278  //**********************************************************************************************
279 
280  //**Friend declarations*************************************************************************
282  friend class ThreadPool;
284  //**********************************************************************************************
285 };
286 //*************************************************************************************************
287 
288 
289 
290 
291 //=================================================================================================
292 //
293 // CONSTRUCTORS
294 //
295 //=================================================================================================
296 
297 //*************************************************************************************************
305 template< typename Callable > // Type of the function/functor
306 inline Thread::Thread( Callable func )
307  : pool_ ( 0 ) // Handle to the managing thread pool
308  , thread_( 0 ) // Handle to the thread of execution
309 {
310  thread_.reset( new boost::thread( func ) );
311 }
312 //*************************************************************************************************
313 
314 
315 //*************************************************************************************************
325 template< typename Callable // Type of the function/functor
326  , typename A1 > // Type of the first argument
327 inline Thread::Thread( Callable func, A1 a1 )
328  : pool_ ( 0 ) // Handle to the managing thread pool
329  , thread_( 0 ) // Handle to the thread of execution
330 {
331  thread_.reset( new boost::thread( func, a1 ) );
332 }
333 //*************************************************************************************************
334 
335 
336 //*************************************************************************************************
347 template< typename Callable // Type of the function/functor
348  , typename A1 // Type of the first argument
349  , typename A2 > // Type of the second argument
350 inline Thread::Thread( Callable func, A1 a1, A2 a2 )
351  : pool_ ( 0 ) // Handle to the managing thread pool
352  , thread_( 0 ) // Handle to the thread of execution
353 {
354  thread_.reset( new boost::thread( func, a1, a2 ) );
355 }
356 //*************************************************************************************************
357 
358 
359 //*************************************************************************************************
371 template< typename Callable // Type of the function/functor
372  , typename A1 // Type of the first argument
373  , typename A2 // Type of the second argument
374  , typename A3 > // Type of the third argument
375 inline Thread::Thread( Callable func, A1 a1, A2 a2, A3 a3 )
376  : pool_ ( 0 ) // Handle to the managing thread pool
377  , thread_( 0 ) // Handle to the thread of execution
378 {
379  thread_.reset( new boost::thread( func, a1, a2, a3 ) );
380 }
381 //*************************************************************************************************
382 
383 
384 //*************************************************************************************************
397 template< typename Callable // Type of the function/functor
398  , typename A1 // Type of the first argument
399  , typename A2 // Type of the second argument
400  , typename A3 // Type of the third argument
401  , typename A4 > // Type of the fourth argument
402 inline Thread::Thread( Callable func, A1 a1, A2 a2, A3 a3, A4 a4 )
403  : pool_ ( 0 ) // Handle to the managing thread pool
404  , thread_( 0 ) // Handle to the thread of execution
405 {
406  thread_.reset( new boost::thread( func, a1, a2, a3, a4 ) );
407 }
408 //*************************************************************************************************
409 
410 
411 //*************************************************************************************************
425 template< typename Callable // Type of the function/functor
426  , typename A1 // Type of the first argument
427  , typename A2 // Type of the second argument
428  , typename A3 // Type of the third argument
429  , typename A4 // Type of the fourth argument
430  , typename A5 > // Type of the fifth argument
431 inline Thread::Thread( Callable func, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5 )
432  : pool_ ( 0 ) // Handle to the managing thread pool
433  , thread_( 0 ) // Handle to the thread of execution
434 {
435  thread_.reset( new boost::thread( func, a1, a2, a3, a4, a5 ) );
436 }
437 //*************************************************************************************************
438 
439 
440 
441 
442 //=================================================================================================
443 //
444 // UTILITY FUNCTIONS
445 //
446 //=================================================================================================
447 
448 //*************************************************************************************************
457 inline bool Thread::joinable() const
458 {
459  return thread_->joinable();
460 }
461 //*************************************************************************************************
462 
463 
464 //*************************************************************************************************
472 inline void Thread::join()
473 {
474  thread_->join();
475 }
476 //*************************************************************************************************
477 
478 
479 //*************************************************************************************************
487 inline bool Thread::hasTerminated() const
488 {
489  return terminated_;
490 }
491 //*************************************************************************************************
492 
493 } // namespace blaze
494 
495 #endif
Base class for non-copyable class instances.
~Thread()
Destructor for the Thread class.
Definition: Thread.cpp:90
ThreadHandle thread_
Handle to the thread of execution.
Definition: Thread.h:276
Base class for non-copyable class instances.The NonCopyable class is intended to work as a base class...
Definition: NonCopyable.h:63
ThreadPool * pool_
Handle to the managing thread pool.
Definition: Thread.h:275
Implementation of a single thread of execution.
Definition: Thread.h:200
void run()
Execution function for threads in a thread pool.
Definition: Thread.cpp:108
bool joinable() const
Returns whether this is a thread of execution.
Definition: Thread.h:457
volatile bool terminated_
Thread termination flag.
Definition: Thread.h:271
boost::scoped_ptr< boost::thread > ThreadHandle
Handle for a single thread.
Definition: Thread.h:204
Thread(ThreadPool *pool)
Starting a thread in a thread pool.
Definition: Thread.cpp:69
void join()
Waiting for a thread of execution to complete.
Definition: Thread.h:472
bool hasTerminated() const
Returns whether the thread has terminated its execution.
Definition: Thread.h:487
Implementation of a thread pool.
Definition: ThreadPool.h:232