Skip to content

About condition usage #1

@dourgulf

Description

@dourgulf

I thinks, condition.notify_one() must be call in mutex lock scope. Why you try to avoid it?
(usage reference :http://en.cppreference.com/w/cpp/thread/condition_variable)

void thread_pool::push_task_with_priority(const dispatch::function& task, queue::priority priority) {
    {
        std::unique_lock<std::mutex> lock(mutex);

        auto queue = queues[priority];
        if (!queue) {
            queue = std::make_shared<queue_impl>(priority);
            queues[priority] = queue;
        }
        
        queue->tasks.push(task);
        
        unsigned max_number_of_threads = std::max<unsigned>(std::thread::hardware_concurrency(), 2);
        unsigned number_of_threads_required = round(log(queues.size()) + 1);
        while (threads.size() < std::min<unsigned>(max_number_of_threads, number_of_threads_required)) {
            add_worker();
        }
    }
    condition.notify_one();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions