parent
18fc34da22
commit
8273deff62
2 changed files with 0 additions and 98 deletions
@ -1,61 +0,0 @@ |
||||
#include "functionpool.hpp" |
||||
|
||||
#include <iostream> |
||||
|
||||
namespace kb { |
||||
namespace viz2d { |
||||
namespace detail { |
||||
|
||||
FunctionPool::FunctionPool() : |
||||
m_function_queue(), m_lock(), m_data_condition(), m_accept_functions(true) { |
||||
} |
||||
|
||||
FunctionPool::~FunctionPool() { |
||||
} |
||||
|
||||
void FunctionPool::push(std::function<void()> func) { |
||||
std::unique_lock<std::mutex> lock(m_lock); |
||||
m_function_queue.push(func); |
||||
// when we send the notification immediately, the consumer will try to get the lock , so unlock asap
|
||||
lock.unlock(); |
||||
m_data_condition.notify_one(); |
||||
} |
||||
|
||||
void FunctionPool::done() { |
||||
std::unique_lock<std::mutex> lock(m_lock); |
||||
m_accept_functions = false; |
||||
lock.unlock(); |
||||
// when we send the notification immediately, the consumer will try to get the lock , so unlock asap
|
||||
m_data_condition.notify_all(); |
||||
//notify all waiting threads.
|
||||
} |
||||
|
||||
void FunctionPool::infinite_loop_func() { |
||||
std::function<void()> func; |
||||
while (true) { |
||||
try { |
||||
{ |
||||
std::unique_lock<std::mutex> lock(m_lock); |
||||
m_data_condition.wait(lock, [this]() { |
||||
return !m_function_queue.empty() || !m_accept_functions; |
||||
}); |
||||
if (!m_accept_functions && m_function_queue.empty()) { |
||||
//lock will be release automatically.
|
||||
//finish the thread loop and let it join in the main thread.
|
||||
return; |
||||
} |
||||
func = m_function_queue.front(); |
||||
m_function_queue.pop(); |
||||
//release the lock
|
||||
} |
||||
func(); |
||||
} catch(std::exception& ex) { |
||||
std::cerr << ex.what() << std::endl; |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
} /* namespace detail */ |
||||
} /* namespace viz2d */ |
||||
} /* namespace kb */ |
@ -1,37 +0,0 @@ |
||||
//started with https://stackoverflow.com/a/51400041/1884837
|
||||
#ifndef SRC_COMMON_FUNCTIONPOOL_HPP_ |
||||
#define SRC_COMMON_FUNCTIONPOOL_HPP_ |
||||
|
||||
#include <queue> |
||||
#include <functional> |
||||
#include <mutex> |
||||
#include <condition_variable> |
||||
#include <atomic> |
||||
#include <cassert> |
||||
|
||||
namespace kb { |
||||
namespace viz2d { |
||||
namespace detail { |
||||
|
||||
class FunctionPool { |
||||
|
||||
private: |
||||
std::queue<std::function<void()>> m_function_queue; |
||||
std::mutex m_lock; |
||||
std::condition_variable m_data_condition; |
||||
std::atomic<bool> m_accept_functions; |
||||
|
||||
public: |
||||
|
||||
FunctionPool(); |
||||
~FunctionPool(); |
||||
void push(std::function<void()> func); |
||||
void done(); |
||||
void infinite_loop_func(); |
||||
}; |
||||
|
||||
} /* namespace detail */ |
||||
} /* namespace viz2d */ |
||||
} /* namespace kb */ |
||||
|
||||
#endif /* SRC_COMMON_FUNCTIONPOOL_HPP_ */ |
Loading…
Reference in new issue