summaryrefslogtreecommitdiff
path: root/job.cpp
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2018-08-29 22:51:58 +0200
committertomsmeding <tom.smeding@gmail.com>2018-08-29 22:51:58 +0200
commit34ab0451bdd1e7496d607acb6ee209586ee7e86e (patch)
treeadf3a587581418bf381fd3b98f38fc55d192b0f2 /job.cpp
parent9da9ab3e4517328ffe0fd0aaf23e2056b705ad76 (diff)
Rename job.{cpp,h} to scheduler
Diffstat (limited to 'job.cpp')
-rw-r--r--job.cpp62
1 files changed, 0 insertions, 62 deletions
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 <iostream>
-#include <chrono>
-#include <cassert>
-#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<void()> &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;
-}