-
Notifications
You must be signed in to change notification settings - Fork 4
Feature thread pool #225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature thread pool #225
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #include "ThreadPool.hpp" | ||
|
|
||
| #include <algorithm> | ||
| #include <stdexcept> | ||
|
|
||
| namespace dsm { | ||
| ThreadPool::ThreadPool(const unsigned int nThreads) : m_stop(false), m_nActiveTasks{0} { | ||
Check noticeCode scanning / Cppcheck (reported by Codacy) MISRA 12.3 rule Note
MISRA 12.3 rule
|
||
| for (size_t i = 0; i < nThreads; ++i) { | ||
| m_threads.emplace_back([this]() { | ||
| while (true) { | ||
| std::function<void()> task; | ||
| { | ||
| std::unique_lock<std::mutex> lock(m_mutex); | ||
| m_cv.wait(lock, [this]() { return m_stop || !m_tasks.empty(); }); | ||
Check noticeCode scanning / Cppcheck (reported by Codacy) MISRA 15.5 rule Note
MISRA 15.5 rule
|
||
|
|
||
| if (m_stop && m_tasks.empty()) { | ||
| return; | ||
Check noticeCode scanning / Cppcheck (reported by Codacy) MISRA 15.5 rule Note
MISRA 15.5 rule
|
||
| } | ||
|
|
||
| task = std::move(m_tasks.front()); | ||
| m_tasks.pop(); | ||
| } | ||
| task(); | ||
| { | ||
| std::unique_lock<std::mutex> lock(m_mutex); | ||
| if (--m_nActiveTasks == 0) { | ||
| m_cv.notify_all(); // Notify that all tasks are done | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| ThreadPool::~ThreadPool() { | ||
| { | ||
| std::unique_lock<std::mutex> lock(m_mutex); | ||
| m_stop = true; | ||
| m_cv.notify_all(); | ||
| } | ||
| for (auto& thread : m_threads) { | ||
| thread.join(); | ||
| } | ||
| } | ||
|
|
||
| void ThreadPool::enqueue(std::function<void()> task) { | ||
| { | ||
| std::unique_lock<std::mutex> lock(m_mutex); | ||
| ++m_nActiveTasks; | ||
| m_tasks.emplace(std::move(task)); | ||
Check noticeCode scanning / Cppcheck (reported by Codacy) MISRA 17.7 rule Note
MISRA 17.7 rule
|
||
| } | ||
| m_cv.notify_one(); | ||
| } | ||
|
|
||
| void ThreadPool::waitAll() { | ||
| std::unique_lock<std::mutex> lock(m_mutex); | ||
| m_cv.wait(lock, [this]() { return m_nActiveTasks == 0; }); | ||
Check noticeCode scanning / Cppcheck (reported by Codacy) MISRA 15.5 rule Note
MISRA 15.5 rule
|
||
| } | ||
| } // namespace dsm | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #pragma once | ||
|
|
||
| #include "../utility/Typedef.hpp" | ||
|
|
||
| #include <atomic> | ||
| #include <condition_variable> | ||
| #include <functional> | ||
| #include <mutex> | ||
| #include <queue> | ||
| #include <thread> | ||
| #include <vector> | ||
|
|
||
| namespace dsm { | ||
| class ThreadPool { | ||
| private: | ||
| bool m_stop; | ||
| std::atomic<size_t> m_nActiveTasks; | ||
| std::vector<std::thread> m_threads; | ||
| std::queue<std::function<void()>> m_tasks; | ||
| std::mutex m_mutex; | ||
| std::condition_variable m_cv; | ||
|
|
||
| public: | ||
| ThreadPool(const unsigned int nThreads = std::thread::hardware_concurrency()); | ||
Check noticeCode scanning / Cppcheck (reported by Codacy) MISRA 13.4 rule Note
MISRA 13.4 rule
Check noticeCode scanning / Cppcheck (reported by Codacy) MISRA 17.8 rule Note
MISRA 17.8 rule
|
||
| ~ThreadPool(); | ||
|
|
||
| void enqueue(std::function<void()> task); | ||
| void waitAll(); | ||
| }; | ||
| } // namespace dsm | ||
Check notice
Code scanning / Cppcheck (reported by Codacy)
MISRA 12.3 rule Note