diff --git a/README.md b/README.md new file mode 100644 index 0000000..2f8b06e --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# dispatch +Simple implementation GCD in C++11 diff --git a/dispatch/dispatch.cpp b/dispatch/dispatch.cpp index bc8b582..b4f59f5 100644 --- a/dispatch/dispatch.cpp +++ b/dispatch/dispatch.cpp @@ -23,7 +23,7 @@ namespace dispatch { const queue::priority priority; std::queue tasks; - bool is_running; + bool is_running = false; queue_impl(queue::priority priority): priority(priority){}; }; @@ -33,7 +33,7 @@ namespace dispatch static std::shared_ptr& shared_pool(); virtual ~thread_pool(); - bool stop; + bool stop = false; typedef std::shared_ptr queue_ptr; @@ -76,38 +76,34 @@ namespace dispatch void thread_pool::push_task_with_priority(const dispatch::function& task, queue::priority priority) { - { - std::unique_lock lock(mutex); + std::unique_lock lock(mutex); - auto queue = queues[priority]; - if (!queue) - { - queue = std::make_shared(priority); - queues[priority] = queue; - } - - queue->tasks.push(task); - - unsigned max_number_of_threads = std::max(std::thread::hardware_concurrency(), 2); - unsigned number_of_threads_required = round(log(queues.size()) + 1); - while (threads.size() < std::min(max_number_of_threads, number_of_threads_required)) - { - add_worker(); - } + auto queue = queues[priority]; + if (!queue) + { + queue = std::make_shared(priority); + queues[priority] = queue; + } + + queue->tasks.push(task); + + unsigned max_number_of_threads = std::max(std::thread::hardware_concurrency(), 2); + unsigned number_of_threads_required = round(log(queues.size()) + 1); + while (threads.size() < std::min(max_number_of_threads, number_of_threads_required)) + { + add_worker(); } condition.notify_one(); } void thread_pool::stop_task_in_queue(const queue_ptr& queue) { - { - std::unique_lock lock(mutex); + std::unique_lock lock(mutex); - queue->is_running = false; - if ( queue->tasks.size() ==0 ) - { - queues.erase(queues.find(queue->priority)); - } + queue->is_running = false; + if ( queue->tasks.size() ==0 ) + { + queues.erase(queues.find(queue->priority)); } condition.notify_one(); }