Example #1
0
void ThreadGroup::schedule(TaskPtr task)
{
    apex_assert_hard(!destroyed_);

    std::unique_lock<std::recursive_mutex> tasks_lock(tasks_mtx_);

    if (!tasks_.empty()) {
        //        for(const TaskPtr& t : tasks_) {
        for (auto it = tasks_.begin(); it != tasks_.end(); ++it) {
            const TaskPtr& t = *it;
            if (t.get() == task.get()) {
                return;
            }
        }
    }

    tasks_.insert(task);

    task->setScheduled(true);

    work_available_.notify_all();
}
Example #2
0
bool ThreadGroup::executeNextTask()
{
    std::unique_lock<std::recursive_mutex> tasks_lock(tasks_mtx_);
    if (!tasks_.empty()) {
        TaskPtr task = *tasks_.begin();
        tasks_.erase(tasks_.begin());

        task->setScheduled(false);

        tasks_lock.unlock();

        {
            std::unique_lock<std::recursive_mutex> state_lock(state_mtx_);
            if (running_) {
                state_lock.unlock();

                executeTask(task);
                return true;
            }
        }
    }

    return false;
}