All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thread.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_THREAD_H_
23 #define _BLAZE_UTIL_THREAD_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <boost/scoped_ptr.hpp>
31 #include <boost/thread/thread.hpp>
32 #include <blaze/util/NonCopyable.h>
33 
34 
35 namespace blaze {
36 
37 //=================================================================================================
38 //
39 // ::blaze NAMESPACE FORWARD DECLARATIONS
40 //
41 //=================================================================================================
42 
43 class ThreadPool;
44 
45 
46 
47 
48 //=================================================================================================
49 //
50 // CLASS DEFINITION
51 //
52 //=================================================================================================
53 
54 //*************************************************************************************************
187 class Thread : private NonCopyable
188 {
189  private:
190  //**Type definitions****************************************************************************
191  typedef boost::scoped_ptr<boost::thread> ThreadHandle;
192  //**********************************************************************************************
193 
194  //**Constructors********************************************************************************
197  explicit Thread( ThreadPool* pool );
199  //**********************************************************************************************
200 
201  public:
202  //**Constructors********************************************************************************
205  template< typename Callable >
206  explicit inline Thread( Callable func );
207 
208  template< typename Callable, typename A1 >
209  explicit inline Thread( Callable func, A1 a1 );
210 
211  template< typename Callable, typename A1, typename A2 >
212  explicit inline Thread( Callable func, A1 a1, A2 a2 );
213 
214  template< typename Callable, typename A1, typename A2, typename A3 >
215  explicit inline Thread( Callable func, A1 a1, A2 a2, A3 a3 );
216 
217  template< typename Callable, typename A1, typename A2, typename A3, typename A4 >
218  explicit inline Thread( Callable func, A1 a1, A2 a2, A3 a3, A4 a4 );
219 
220  template< typename Callable, typename A1, typename A2, typename A3, typename A4, typename A5 >
221  explicit inline Thread( Callable func, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5 );
223  //**********************************************************************************************
224 
225  //**Destructor**********************************************************************************
228  ~Thread();
230  //**********************************************************************************************
231 
232  //**Utility functions***************************************************************************
235  inline bool joinable() const;
236  inline void join();
238  //**********************************************************************************************
239 
240  private:
241  //**Utility functions***************************************************************************
244  inline bool hasTerminated() const;
246  //**********************************************************************************************
247 
248  //**Thread execution functions******************************************************************
251  void run();
253  //**********************************************************************************************
254 
255  //**Member variables****************************************************************************
258  volatile bool terminated_;
259 
264 
265  //**********************************************************************************************
266 
267  //**Friend declarations*************************************************************************
269  friend class ThreadPool;
271  //**********************************************************************************************
272 };
273 //*************************************************************************************************
274 
275 
276 
277 
278 //=================================================================================================
279 //
280 // CONSTRUCTORS
281 //
282 //=================================================================================================
283 
284 //*************************************************************************************************
292 template< typename Callable > // Type of the function/functor
293 inline Thread::Thread( Callable func )
294  : pool_ ( 0 ) // Handle to the managing thread pool
295  , thread_( 0 ) // Handle to the thread of execution
296 {
297  thread_.reset( new boost::thread( func ) );
298 }
299 //*************************************************************************************************
300 
301 
302 //*************************************************************************************************
312 template< typename Callable // Type of the function/functor
313  , typename A1 > // Type of the first argument
314 inline Thread::Thread( Callable func, A1 a1 )
315  : pool_ ( 0 ) // Handle to the managing thread pool
316  , thread_( 0 ) // Handle to the thread of execution
317 {
318  thread_.reset( new boost::thread( func, a1 ) );
319 }
320 //*************************************************************************************************
321 
322 
323 //*************************************************************************************************
334 template< typename Callable // Type of the function/functor
335  , typename A1 // Type of the first argument
336  , typename A2 > // Type of the second argument
337 inline Thread::Thread( Callable func, A1 a1, A2 a2 )
338  : pool_ ( 0 ) // Handle to the managing thread pool
339  , thread_( 0 ) // Handle to the thread of execution
340 {
341  thread_.reset( new boost::thread( func, a1, a2 ) );
342 }
343 //*************************************************************************************************
344 
345 
346 //*************************************************************************************************
358 template< typename Callable // Type of the function/functor
359  , typename A1 // Type of the first argument
360  , typename A2 // Type of the second argument
361  , typename A3 > // Type of the third argument
362 inline Thread::Thread( Callable func, A1 a1, A2 a2, A3 a3 )
363  : pool_ ( 0 ) // Handle to the managing thread pool
364  , thread_( 0 ) // Handle to the thread of execution
365 {
366  thread_.reset( new boost::thread( func, a1, a2, a3 ) );
367 }
368 //*************************************************************************************************
369 
370 
371 //*************************************************************************************************
384 template< typename Callable // Type of the function/functor
385  , typename A1 // Type of the first argument
386  , typename A2 // Type of the second argument
387  , typename A3 // Type of the third argument
388  , typename A4 > // Type of the fourth argument
389 inline Thread::Thread( Callable func, A1 a1, A2 a2, A3 a3, A4 a4 )
390  : pool_ ( 0 ) // Handle to the managing thread pool
391  , thread_( 0 ) // Handle to the thread of execution
392 {
393  thread_.reset( new boost::thread( func, a1, a2, a3, a4 ) );
394 }
395 //*************************************************************************************************
396 
397 
398 //*************************************************************************************************
412 template< typename Callable // Type of the function/functor
413  , typename A1 // Type of the first argument
414  , typename A2 // Type of the second argument
415  , typename A3 // Type of the third argument
416  , typename A4 // Type of the fourth argument
417  , typename A5 > // Type of the fifth argument
418 inline Thread::Thread( Callable func, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5 )
419  : pool_ ( 0 ) // Handle to the managing thread pool
420  , thread_( 0 ) // Handle to the thread of execution
421 {
422  thread_.reset( new boost::thread( func, a1, a2, a3, a4, a5 ) );
423 }
424 //*************************************************************************************************
425 
426 
427 
428 
429 //=================================================================================================
430 //
431 // UTILITY FUNCTIONS
432 //
433 //=================================================================================================
434 
435 //*************************************************************************************************
444 inline bool Thread::joinable() const
445 {
446  return thread_->joinable();
447 }
448 //*************************************************************************************************
449 
450 
451 //*************************************************************************************************
459 inline void Thread::join()
460 {
461  thread_->join();
462 }
463 //*************************************************************************************************
464 
465 
466 //*************************************************************************************************
474 inline bool Thread::hasTerminated() const
475 {
476  return terminated_;
477 }
478 //*************************************************************************************************
479 
480 } // namespace blaze
481 
482 #endif