Skip to content

Commit

Permalink
Added some comments, moved lock_guard to function that has to lock it.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bosma committed Mar 2, 2018
1 parent 3c0a656 commit 562b9f6
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ namespace Bosma {
auto time_of_first_task = (*tasks.begin()).first;
sleeper.sleep_until(time_of_first_task);
}
std::lock_guard<std::mutex> l(lock);
manage_tasks();
}
});
Expand Down Expand Up @@ -178,20 +177,26 @@ namespace Bosma {
}

void manage_tasks() {
std::lock_guard<std::mutex> l(lock);

auto end_of_tasks_to_run = tasks.upper_bound(Clock::now());

// if there are any tasks to be run and removed
if (end_of_tasks_to_run != tasks.begin()) {
// keep track of tasks that will be re-added
decltype(tasks) recurred_tasks;

// for all tasks that have been triggered
for (auto i = tasks.begin(); i != end_of_tasks_to_run; ++i) {

auto &task = (*i).second;

if (task->interval) {
// if it's an interval task, add the task back after f() is completed
// if it's an interval task, only add the task back after f() is completed
threads.push([this, task](int) {
task->f();
// no risk of race-condition,
// add_task() will wait for manage_tasks() to release lock
add_task(task->get_new_time(), task);
});
} else {
Expand Down

0 comments on commit 562b9f6

Please sign in to comment.