Skip to content

Commit

Permalink
free pool when destructing (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jul 11, 2023
1 parent fa92e01 commit aa0c4df
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions include/ylt/coro_io/io_context_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <atomic>
#include <future>
#include <memory>
#include <mutex>
#include <thread>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -106,6 +107,12 @@ class io_context_pool {
}

void run() {
bool has_run_or_stop = false;
bool ok = has_run_or_stop_.compare_exchange_strong(has_run_or_stop, true);
if (!ok) {
return;
}

std::vector<std::shared_ptr<std::thread>> threads;
for (std::size_t i = 0; i < io_contexts_.size(); ++i) {
threads.emplace_back(std::make_shared<std::thread>(
Expand All @@ -122,15 +129,24 @@ class io_context_pool {
}

void stop() {
work_.clear();
promise_.get_future().wait();
return;
std::call_once(flag_, [this] {
bool has_run_or_stop = false;
bool ok = has_run_or_stop_.compare_exchange_strong(has_run_or_stop, true);

work_.clear();

if (ok) {
return;
}

promise_.get_future().wait();
});
}

// ~io_context_pool() {
// if (!has_stop())
// stop();
// }
~io_context_pool() {
if (!has_stop())
stop();
}

std::size_t pool_size() const noexcept { return io_contexts_.size(); }

Expand All @@ -156,6 +172,8 @@ class io_context_pool {
std::vector<work_ptr> work_;
std::atomic<std::size_t> next_io_context_;
std::promise<void> promise_;
std::atomic<bool> has_run_or_stop_ = false;
std::once_flag flag_;
};

class multithread_context_pool {
Expand Down

0 comments on commit aa0c4df

Please sign in to comment.