From 34ab0451bdd1e7496d607acb6ee209586ee7e86e Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 29 Aug 2018 22:51:58 +0200 Subject: Rename job.{cpp,h} to scheduler --- job.cpp | 62 ----------------------------------------------------------- job.h | 40 -------------------------------------- main.cpp | 2 +- scheduler.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ scheduler.h | 40 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 103 deletions(-) delete mode 100644 job.cpp delete mode 100644 job.h create mode 100644 scheduler.cpp create mode 100644 scheduler.h diff --git a/job.cpp b/job.cpp deleted file mode 100644 index 90fede2..0000000 --- a/job.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -#include -#include "job.h" - -void Scheduler::workerEntry() { - while (true) { - Job *job = nullptr; - - { - lock_guard commMutGuard(commMut); - if (jobs.size() > 0) { - job = jobs.front(); - jobs.pop(); - } - } - - if (job) { - job->callback(); - } else if (finishFlag) { - break; - } else { - this_thread::sleep_for(chrono::milliseconds(100)); - } - } -} - -Scheduler::Scheduler(int nthreads) - : nthreads(nthreads) { - - assert(nthreads > 0); - - workers.reserve(nthreads); - for (int i = 0; i < nthreads; i++) { - workers.emplace_back([this]() { workerEntry(); }); - } -} - -Scheduler::~Scheduler() { - finish(); -} - -void Scheduler::submit(const function &func) { - Job *job = new Job(func); - lock_guard commMutGuard(commMut); - jobs.push(job); -} - -void Scheduler::finish() { - if (hasJoined) return; - - { - lock_guard commMutGuard(commMut); - finishFlag = true; - } - - for (int i = 0; i < nthreads; i++) { - workers[i].join(); - } - - hasJoined = true; -} diff --git a/job.h b/job.h deleted file mode 100644 index 95da7c7..0000000 --- a/job.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -using namespace std; - - -class Scheduler { - struct Job { - function callback; - - Job(const function callback) - : callback(callback) {} - }; - - queue jobs; - bool finishFlag = false; - mutex commMut; - - bool hasJoined = false; - - vector workers; - - void workerEntry(); - -public: - const int nthreads; - - Scheduler(int nthreads); - ~Scheduler(); - - // func is run in child thread - void submit(const function &func); - - void finish(); -}; diff --git a/main.cpp b/main.cpp index 3a801ba..173346c 100644 --- a/main.cpp +++ b/main.cpp @@ -17,7 +17,7 @@ #include #include #include -#include "job.h" +#include "scheduler.h" #include "process.h" #include "error.h" #include "multilog.h" diff --git a/scheduler.cpp b/scheduler.cpp new file mode 100644 index 0000000..452b0bb --- /dev/null +++ b/scheduler.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include "scheduler.h" + +void Scheduler::workerEntry() { + while (true) { + Job *job = nullptr; + + { + lock_guard commMutGuard(commMut); + if (jobs.size() > 0) { + job = jobs.front(); + jobs.pop(); + } + } + + if (job) { + job->callback(); + } else if (finishFlag) { + break; + } else { + this_thread::sleep_for(chrono::milliseconds(100)); + } + } +} + +Scheduler::Scheduler(int nthreads) + : nthreads(nthreads) { + + assert(nthreads > 0); + + workers.reserve(nthreads); + for (int i = 0; i < nthreads; i++) { + workers.emplace_back([this]() { workerEntry(); }); + } +} + +Scheduler::~Scheduler() { + finish(); +} + +void Scheduler::submit(const function &func) { + Job *job = new Job(func); + lock_guard commMutGuard(commMut); + jobs.push(job); +} + +void Scheduler::finish() { + if (hasJoined) return; + + { + lock_guard commMutGuard(commMut); + finishFlag = true; + } + + for (int i = 0; i < nthreads; i++) { + workers[i].join(); + } + + hasJoined = true; +} diff --git a/scheduler.h b/scheduler.h new file mode 100644 index 0000000..95da7c7 --- /dev/null +++ b/scheduler.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include +#include +#include +#include + +using namespace std; + + +class Scheduler { + struct Job { + function callback; + + Job(const function callback) + : callback(callback) {} + }; + + queue jobs; + bool finishFlag = false; + mutex commMut; + + bool hasJoined = false; + + vector workers; + + void workerEntry(); + +public: + const int nthreads; + + Scheduler(int nthreads); + ~Scheduler(); + + // func is run in child thread + void submit(const function &func); + + void finish(); +}; -- cgit v1.2.3-70-g09d2