mirror of
https://github.com/ProgramSnail/build_system_2022.git
synced 2025-12-06 00:48:42 +00:00
init
This commit is contained in:
parent
4d899f64a7
commit
b4a7121411
23 changed files with 681 additions and 11 deletions
47
thread_pool.cpp
Normal file
47
thread_pool.cpp
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#include "thread_pool.hpp"
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include "task.hpp"
|
||||
|
||||
namespace build_system {
|
||||
|
||||
void ThreadPool::addTarget(Task&& task, size_t id,
|
||||
const std::vector<size_t>& dependences) {
|
||||
size_t new_id = dependency_manager_.add(id, dependences);
|
||||
task_queue_.addIdentTask({std::move(task), new_id});
|
||||
}
|
||||
|
||||
void ThreadPool::start() {
|
||||
threads_.reserve(num_threads_);
|
||||
for (size_t i = 0; i < num_threads_; ++i) {
|
||||
threads_.emplace_back([this]() {
|
||||
while (true) {
|
||||
auto may_be_task = task_queue_.getIdentTask();
|
||||
if (may_be_task == std::nullopt) {
|
||||
break;
|
||||
}
|
||||
|
||||
dependency_manager_.wait(may_be_task.value().second);
|
||||
|
||||
try {
|
||||
may_be_task.value().first();
|
||||
} catch (...) {
|
||||
}
|
||||
|
||||
dependency_manager_.done(may_be_task.value().second);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ThreadPool::wait() {
|
||||
task_queue_.close();
|
||||
dependency_manager_.waitAll();
|
||||
|
||||
for (auto& thread : threads_) {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
}; // namespace build_system
|
||||
Loading…
Add table
Add a link
Reference in a new issue