inital support for parallel load
This commit is contained in:
132
threadpoool.h
Normal file
132
threadpoool.h
Normal file
@@ -0,0 +1,132 @@
|
||||
#include <iostream>
|
||||
|
||||
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
#include <queue>
|
||||
#include <condition_variable>
|
||||
#include <vector>
|
||||
|
||||
class thread_pool {
|
||||
public:
|
||||
thread_pool(std::size_t size) : stop(false), exit_on_empty(false) {
|
||||
for (std::size_t i = 0; i < size; ++i) {
|
||||
workers.emplace_back([this] { spawn(); });
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~thread_pool() {
|
||||
if (!stop) join();
|
||||
}
|
||||
|
||||
// template<class F, class... Args>
|
||||
// void post(F&& f, Args&&... args) {
|
||||
void post(std::function<void()> f) {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
tasks.push(f);
|
||||
}
|
||||
|
||||
condition.notify_one();
|
||||
}
|
||||
|
||||
void join() {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
stop = true;
|
||||
}
|
||||
|
||||
condition.notify_all();
|
||||
|
||||
for (std::size_t i = 0; i < workers.size(); ++i) {
|
||||
workers[i].join();
|
||||
}
|
||||
}
|
||||
|
||||
void finish_tasks() {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
exit_on_empty = true;
|
||||
}
|
||||
|
||||
while (!tasks.empty()) {
|
||||
condition.notify_all();
|
||||
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
condition_empty.wait(lock, [this]() {
|
||||
return (tasks.empty());
|
||||
});
|
||||
}
|
||||
|
||||
// BLEJU, BLEJU
|
||||
// while (!tasks.empty()) condition.notify_all();
|
||||
bool op = true;
|
||||
}
|
||||
|
||||
private:
|
||||
void spawn() {
|
||||
std::function<void()> task;
|
||||
|
||||
while (!stop && (!exit_on_empty || !tasks.empty())) {
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
condition.wait(lock, [this]() {
|
||||
return (!tasks.empty()) || (tasks.empty() && stop);
|
||||
});
|
||||
|
||||
if (!tasks.empty()) {
|
||||
task = std::move(tasks.front());
|
||||
tasks.pop();
|
||||
task();
|
||||
}
|
||||
}
|
||||
if (exit_on_empty) {
|
||||
condition_empty.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
std::vector<std::thread> workers;
|
||||
std::queue<std::function<void()>> tasks;
|
||||
|
||||
std::mutex mutex;
|
||||
std::condition_variable condition;
|
||||
std::condition_variable condition_empty;
|
||||
bool stop;
|
||||
bool exit_on_empty;
|
||||
};
|
||||
|
||||
// template<class F, class... Args>
|
||||
// inline void dispatch(thread_pool& pool, F&& f, Args&&... args) {
|
||||
inline void dispatch(thread_pool& pool, std::function<void()> f) {
|
||||
pool.post(f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// int main() {
|
||||
// int cnt = 0;
|
||||
|
||||
// thread_pool tp{std::thread::hardware_concurrency()};
|
||||
// std::mutex mutex;
|
||||
|
||||
// std::cout << "start" << std::endl;
|
||||
|
||||
// for(int i=0; i<100; i++) {
|
||||
// dispatch(tp, std::function<void()>
|
||||
// ([i, &cnt, &mutex]() {
|
||||
// std::cout << "test " << i << std::endl;
|
||||
// {
|
||||
// std::unique_lock<std::mutex> lock(mutex);
|
||||
// cnt++;
|
||||
// }
|
||||
// }
|
||||
// ));
|
||||
// }
|
||||
|
||||
|
||||
// std::cout << "end" << std::endl;
|
||||
// tp.join();
|
||||
// std::cout << "cnt:" << cnt << std::endl;
|
||||
|
||||
// }
|
||||
Reference in New Issue
Block a user