WIP: thread pool class
For now it's missing ability to, you know, actually run tasks.
This commit is contained in:
parent
a18c475f38
commit
9f1e4564df
5 changed files with 141 additions and 0 deletions
|
@ -3237,6 +3237,7 @@
|
|||
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)Tests\Utils\</ObjectFileName>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\theme.cpp" />
|
||||
<ClCompile Include="..\..\src\thread_pool.cpp" />
|
||||
<ClCompile Include="..\..\src\time_of_day.cpp" />
|
||||
<ClCompile Include="..\..\src\tod_manager.cpp" />
|
||||
<ClCompile Include="..\..\src\tooltips.cpp" />
|
||||
|
@ -4025,6 +4026,7 @@
|
|||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
</CustomBuild>
|
||||
<ClInclude Include="..\..\src\theme.hpp" />
|
||||
<ClInclude Include="..\..\src\thread_pool.hpp" />
|
||||
<ClInclude Include="..\..\src\time_of_day.hpp" />
|
||||
<ClInclude Include="..\..\src\tod_manager.hpp" />
|
||||
<ClInclude Include="..\..\src\serialization\tag.hpp" />
|
||||
|
|
|
@ -1555,6 +1555,7 @@
|
|||
<ClCompile Include="..\..\src\ogl\shader.cpp">
|
||||
<Filter>OGL</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\thread_pool.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\src\addon\client.hpp">
|
||||
|
@ -3029,6 +3030,7 @@
|
|||
<ClInclude Include="..\..\src\ogl\shader.hpp">
|
||||
<Filter>OGL</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\thread_pool.hpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<CustomBuild Include="..\..\src\tests\test_sdl_utils.hpp">
|
||||
|
|
|
@ -347,6 +347,7 @@ syncmp_handler.cpp
|
|||
team.cpp
|
||||
teambuilder.cpp
|
||||
terrain/filter.cpp
|
||||
thread_pool.cpp
|
||||
tod_manager.cpp
|
||||
units/abilities.cpp
|
||||
units/animation.cpp
|
||||
|
|
90
src/thread_pool.cpp
Normal file
90
src/thread_pool.cpp
Normal file
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
Copyright (C) 2018 by Jyrki Vesterinen <sandgtx@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#include "thread_pool.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <iterator>
|
||||
|
||||
thread_pool::thread_pool()
|
||||
: exiting_(false)
|
||||
{
|
||||
init();
|
||||
|
||||
std::generate_n(std::back_inserter(threads_), NUM_THREADS,
|
||||
[this]() { return std::thread(&thread_pool::thread_proc, this); });
|
||||
}
|
||||
|
||||
thread_pool::~thread_pool()
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
|
||||
// A thread pool can't exit if work is in progress.
|
||||
assert(!work_);
|
||||
|
||||
exiting_ = true;
|
||||
work_cond_.notify_all();
|
||||
}
|
||||
|
||||
for(std::thread& t : threads_) {
|
||||
t.join();
|
||||
}
|
||||
}
|
||||
|
||||
void thread_pool::init()
|
||||
{
|
||||
num_waiting_threads_ = 0u;
|
||||
num_finished_threads_ = 0u;
|
||||
ready_for_work_ = false;
|
||||
work_ = nullptr;
|
||||
done_ = false;
|
||||
}
|
||||
|
||||
void thread_pool::thread_proc()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
|
||||
++num_waiting_threads_;
|
||||
if(num_waiting_threads_ == NUM_THREADS) {
|
||||
ready_for_work_ = true;
|
||||
ready_for_work_cond_.notify_one();
|
||||
}
|
||||
|
||||
while(!work_ && !exiting_) {
|
||||
work_cond_.wait(lock);
|
||||
}
|
||||
|
||||
num_finished_threads_ = 0u;
|
||||
lock.unlock();
|
||||
|
||||
while(!exiting_) {
|
||||
work_();
|
||||
|
||||
lock.lock();
|
||||
++num_finished_threads_;
|
||||
if(num_finished_threads_ == NUM_THREADS) {
|
||||
done_ = true;
|
||||
done_promise_.set_value();
|
||||
work_ = nullptr;
|
||||
}
|
||||
|
||||
while(!work_ && !exiting_) {
|
||||
work_cond_.wait(lock);
|
||||
}
|
||||
|
||||
num_finished_threads_ = 0u;
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
46
src/thread_pool.hpp
Normal file
46
src/thread_pool.hpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
Copyright (C) 2018 by Jyrki Vesterinen <sandgtx@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
class thread_pool
|
||||
{
|
||||
thread_pool();
|
||||
~thread_pool();
|
||||
|
||||
private:
|
||||
const unsigned int NUM_THREADS = 16u;
|
||||
|
||||
void thread_proc();
|
||||
|
||||
void init();
|
||||
|
||||
std::vector<std::thread> threads_;
|
||||
std::function<void()> work_;
|
||||
std::mutex mutex_;
|
||||
unsigned int num_waiting_threads_;
|
||||
unsigned int num_finished_threads_;
|
||||
bool ready_for_work_;
|
||||
std::condition_variable ready_for_work_cond_;
|
||||
std::condition_variable work_cond_;
|
||||
bool done_;
|
||||
std::promise<void> done_promise_;
|
||||
bool exiting_;
|
||||
};
|
Loading…
Add table
Reference in a new issue