Fixing up threadpool
This commit is contained in:
@@ -22,7 +22,7 @@ namespace daggy {
|
||||
|
||||
std::future<void> addTask(AsyncTask fn);
|
||||
private:
|
||||
using QueuedAsyncTask = std::shared_ptr<AsyncTask>;
|
||||
using QueuedAsyncTask = std::shared_ptr<std::packaged_task<void()>>;
|
||||
|
||||
std::atomic<bool> shutdown_;
|
||||
std::mutex guard_;
|
||||
|
||||
@@ -13,7 +13,9 @@ ThreadPool::ThreadPool(size_t nWorkers) {
|
||||
std::unique_lock<std::mutex> lk(guard_);
|
||||
cv_.wait(lk, []{ return true; });
|
||||
if (shutdown_) return;
|
||||
tsk.swap(taskQueue_.front());
|
||||
if (taskQueue_.empty()) continue;
|
||||
|
||||
tsk = taskQueue_.front();
|
||||
taskQueue_.pop_front();
|
||||
}
|
||||
|
||||
@@ -41,7 +43,7 @@ std::future<void> ThreadPool::addTask(std::function<void()> fn) {
|
||||
std::future<void> result = task->get_future();
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(guard_);
|
||||
taskQueue_.emplace_back(std::move(task));
|
||||
taskQueue_.push_back(task);
|
||||
}
|
||||
cv_.notify_one();
|
||||
return result;
|
||||
|
||||
@@ -29,13 +29,22 @@ TEST_CASE("Basic Execution", "[forking_executor]") {
|
||||
}
|
||||
|
||||
SECTION("Large Output") {
|
||||
const std::string BIG_FILE{"/usr/share/dict/linux.words"};
|
||||
std::vector<std::string> cmd{"/usr/bin/cat", BIG_FILE};
|
||||
const std::vector<std::string> BIG_FILES{
|
||||
"/usr/share/dict/linux.words"
|
||||
, "/usr/share/dict/cracklib-small"
|
||||
, "/etc/ssh/moduli"
|
||||
};
|
||||
|
||||
auto rec = ex.runCommand(cmd);
|
||||
for (const auto & bigFile : BIG_FILES) {
|
||||
if (! std::filesystem::exists(bigFile)) continue;
|
||||
|
||||
REQUIRE(rec.rc == 0);
|
||||
REQUIRE(rec.output.size() == std::filesystem::file_size(BIG_FILE));
|
||||
REQUIRE(rec.error.empty());
|
||||
std::vector<std::string> cmd{"/usr/bin/cat", bigFile};
|
||||
|
||||
auto rec = ex.runCommand(cmd);
|
||||
|
||||
REQUIRE(rec.rc == 0);
|
||||
REQUIRE(rec.output.size() == std::filesystem::file_size(bigFile));
|
||||
REQUIRE(rec.error.empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,13 +12,20 @@ TEST_CASE("Threadpool Construction", "[threadpool]") {
|
||||
ThreadPool tp(10);
|
||||
|
||||
std::vector<std::future<void>> res;
|
||||
for (size_t i = 0; i < 100; ++i) {
|
||||
res.push_back(tp.addTask([&cnt]() -> void { cnt++; return; }));
|
||||
|
||||
SECTION("Simple runs") {
|
||||
for (size_t i = 0; i < 100; ++i)
|
||||
res.push_back(tp.addTask([&cnt]() { cnt++; return; }));
|
||||
for (auto & r : res) r.get();
|
||||
REQUIRE(cnt == 100);
|
||||
}
|
||||
|
||||
for (auto & r : res) {
|
||||
r.get();
|
||||
SECTION("Slow runs") {
|
||||
using namespace std::chrono_literals;
|
||||
for (size_t i = 0; i < 100; ++i)
|
||||
res.push_back(tp.addTask([&cnt]() { std::this_thread::sleep_for(20ms); cnt++; return; }));
|
||||
for (auto & r : res) r.get();
|
||||
REQUIRE(cnt == 100);
|
||||
}
|
||||
|
||||
REQUIRE(cnt == 100);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user