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 given thread count is 0, it will detect and use all available threads
55  ThreadGroup(std::size_t threadCount = 0);
56 
57  //!< Destructor
59 
60  //!< Gets the number of threads that are defined to be used by this class
61  std::size_t getThreadCount();
62 
63  //!< Creates a new job and binds it to the given function. Use std::bind(function, object, params...) as argument
64  template <typename Function>
65  void addJob(Function func);
66 
67  //!< Waits for all threads to finish their jobs
68  void waitToFinish();
69 
70  private:
71 
72  //!< Starts the execution of the thread group. Note that the jobs must be added before the calling of this function
73  void run();
74 
75  private:
76 
77  std::size_t m_threadCount;
78  std::unique_ptr<boost::asio::io_service> m_ioService; //!< Boost IO service
79  std::unique_ptr<boost::thread_group> m_threadGroup; //!< Boost thread group
80  std::unique_ptr<boost::asio::io_service::work> m_work; //!< Boost work
81  };
82 
83  //implementation of the templated function
84  template <typename Function>
85  void ThreadGroup::addJob(Function func)
86  {
87  m_ioService->post(func);
88  }
89 
90  } // end namespace common
91 } // end namespace te
92 
93 #endif // __TERRALIB_COMMON_INTERNAL_THREADGROUP_H
94 
te::common::ThreadGroup::run
void run()
< Starts the execution of the thread group. Note that the jobs must be added before the calling of th...
te::common::ThreadGroup::m_work
std::unique_ptr< boost::asio::io_service::work > m_work
Boost work.
Definition: ThreadGroup.h:80
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::common::ThreadGroup
This class represents a thread group.
Definition: ThreadGroup.h:51
TECOMMONEXPORT
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:66
te::common::ThreadGroup::addJob
void addJob(Function func)
Waits for all threads to finish their jobs.
Definition: ThreadGroup.h:85
te::common::ThreadGroup::m_ioService
std::unique_ptr< boost::asio::io_service > m_ioService
Boost IO service.
Definition: ThreadGroup.h:78
te::common::ThreadGroup::~ThreadGroup
~ThreadGroup()
Gets the number of threads that are defined to be used by this class.
te::common::ThreadGroup::m_threadCount
std::size_t m_threadCount
Definition: ThreadGroup.h:77
te::common::ThreadGroup::getThreadCount
std::size_t getThreadCount()
Creates a new job and binds it to the given function. Use std::bind(function, object,...
te::common::ThreadGroup::m_threadGroup
std::unique_ptr< boost::thread_group > m_threadGroup
Boost thread group.
Definition: ThreadGroup.h:79
te::common::ThreadGroup::ThreadGroup
ThreadGroup(std::size_t threadCount=0)
< Constructor. If the given thread count is 0, it will detect and use all available threads
Config.h
Proxy configuration file for TerraView (see terraview_config.h).
te::common::ThreadGroup::waitToFinish
void waitToFinish()