AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
threadPool.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <functional>
23#include <thread>
24#include <mutex>
25#include <queue>
26#include <chrono>
27#include <atomic>
28#include <condition_variable>
29
30namespace anch {
31
45 class ThreadPool {
46 // Attributes +
47 private:
49 std::queue<std::function<void()> > _threads;
50
52 std::mutex _mutex;
53
55 uint32_t _maxThreads;
56
58 std::atomic<uint32_t> _available;
59
61 bool _running;
62
64 std::atomic<bool> _terminating;
65
67 std::condition_variable _termCV;
68 // Attributes -
69
70 // Constructors +
71 public:
77 ThreadPool(uint32_t maxThreads = 0);
78 // Constructors -
79
80 // Destructor +
81 public:
85 virtual ~ThreadPool();
86 // Destructor -
87
88 // Methods +
89 public:
93 void start();
94
98 void stop();
99
105 template<typename R, typename P = std::ratio<1>>
106 void awaitTermination(std::chrono::duration<R,P> duration);
107
114 template<typename Func, typename... Args>
115 void add(Func function, Args... args);
116
117 private:
121 void launchTreatment(std::function<void()> func);
122
126 void afterExecute();
127
133 void execute(std::function<void()> func);
134 // Methods -
135
136 };
137
138}
139
140#include "impl/threadPool.hpp"
void awaitTermination(std::chrono::duration< R, P > duration)
ThreadPool(uint32_t maxThreads=0)
void add(Func function, Args... args)
virtual ~ThreadPool()
AnCH framework base namespace.
Definition app.hpp:28