ThreadGroup.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20  /*!
21  \file terralib/common/ThreadGroup.h
22 
23  \brief This class represents a thread group
24 
25  \ingroup common
26  */
27 
28 #ifndef __TERRALIB_COMMON_INTERNAL_THREADGROUP_H
29 #define __TERRALIB_COMMON_INTERNAL_THREADGROUP_H
30 
31  // TerraLib
32 #include "Config.h"
33 
34 //Boost include files
35 #include <boost/thread.hpp>
36 #include <boost/asio/io_service.hpp>
37 
38 //STL
39 #include <functional>
40 #include <string>
41 
42 namespace te
43 {
44  namespace common
45  {
46  /*!
47  \class ThreadGroup
48  \brief This class represents a thread group
49  */
51  {
52  public:
53 
54  //!< Constructor. If the number of thread is not especified, it will use all available threads
55  ThreadGroup(std::size_t numberOfThreads = std::string::npos);
56 
57  //!< Destructor
58  ~ThreadGroup();
59 
60  //!< Creates a new job and binds it to the given function. Use std::bind(function, object, params...) as argument
61  template <typename Function>
62  void addJob(Function func);
63 
64  //!< Waits for all threads to finish their jobs
65  void waitToFinish();
66 
67  protected:
68 
69  std::unique_ptr<boost::asio::io_service> m_ioService; //!< Boost IO service
70  std::unique_ptr<boost::thread_group> m_threadGroup; //!< Boost thread group
71  std::unique_ptr<boost::asio::io_service::work> m_work; //!< Boost work
72  };
73 
74  //implementation of the templated function
75  template <typename Function>
76  void ThreadGroup::addJob(Function func)
77  {
78  m_ioService->post(func);
79  }
80 
81  } // end namespace common
82 } // end namespace te
83 
84 #endif // __TERRALIB_COMMON_INTERNAL_THREADGROUP_H
85 
This class represents a thread group.
Definition: ThreadGroup.h:50
Configuration flags for the TerraLib Common Runtime module.
TerraLib.
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:66
void addJob(Function func)
Waits for all threads to finish their jobs.
Definition: ThreadGroup.h:76
std::unique_ptr< boost::asio::io_service > m_ioService
Boost IO service.
Definition: ThreadGroup.h:69
std::unique_ptr< boost::thread_group > m_threadGroup
Boost thread group.
Definition: ThreadGroup.h:70
std::unique_ptr< boost::asio::io_service::work > m_work
Boost work.
Definition: ThreadGroup.h:71