TFCweb  1.0.4 $Rev: 483 $
TFC Primavera 2012: Nucli d'un servidor web
ThreadPool.h
Veure la documentació d'aquest fitxer.
1 #if !defined(_TFCWEB_THREADPOOL_H_)
2 #define _TFCWEB_THREADPOOL_H_
3 
10 /*
11  * Copyright (c) 2012 Toni Corvera
12  *
13  * This file is part of TFCWeb.
14  *
15  * TFCWeb is free software: you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation, either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * TFCWeb is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with TFCWeb. If not, see <http://www.gnu.org/licenses/>.
27  */
28 
29 #include "CuaConcurrent.h"
30 #include "Excepcions.h"
31 #include "utils.h"
32 
33 #include <cstddef>
34 #include <queue>
35 #include <vector>
36 #include <boost/any.hpp>
37 #include <boost/function.hpp>
38 #include <boost/thread.hpp>
39 
40 namespace tfc {
41 
43 public:
44  explicit ErrorThreadPoolTancada(const std::string & w = "Thread pool tancada")
45  : ErrorTFC(w)
46  {
47  // buit
48  }
49 };
50 
63 class ThreadPool {
64 public:
65  // void fn() -> function<void (void)>
66  typedef boost::function<void (void)> tipus_tasca;
68 
76  explicit ThreadPool(const size_t n_workers);
77 
78  virtual ~ThreadPool();
79 
90 
92  void tanca();
93 
95  bool es_oberta() const { return !tancada_; }
96 
100  void join();
101 
106  void termina();
107 
111  size_t num_tasques() const {
112  return tasques_.size();
113  }
114 
115 private:
116  void inicia();
117 
118  const size_t n_threads_;
119  std::vector< boost::thread * > threads_;
121  bool tancada_;
122  volatile bool marca_terminacio_;
123 };
124 
125 } // ns tfc
126 
127 #endif // _TFCWEB_THREADPOOL_H_
128 
129 // vim:set ts=4 et ai: //