Misc (#19)
* get image returns optional<Mat> instead of unique_ptr<Mat> * introduce complexity * rename image load function * add example for a basic reader for binary image files * fix windows build? * add a status bar underneath the menu bar * use status bar in custom_0 example app * clang formats * packing w/ pragma push * add "Save As" functions to image views * resize over reserve * rm local override uri * add simple thread pool * rename to simple_thread_pool * get rid of useless renderlib * document version inc * extract hardcoded values to in-header * clang format patch * clone registered image in custom_0 * document binary-file header * minor fixes * clang format * rm unused render cmake
This commit is contained in:
committed by
Maximilian Kueffner
parent
e3e161ce52
commit
b37814204f
@@ -3,7 +3,9 @@ set(UTILSLIBNAME pixelariumutilslib)
|
||||
set(UTILSLIBSRC
|
||||
ILog.hpp
|
||||
SpdLogger.hpp
|
||||
SpdLogger.cpp)
|
||||
SpdLogger.cpp
|
||||
simple_thread_pool.hpp
|
||||
simple_thread_pool.cpp)
|
||||
|
||||
add_library(${UTILSLIBNAME} STATIC ${UTILSLIBSRC})
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "simple_thread_pool.hpp"
|
||||
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
|
||||
using namespace pixelarium::utils;
|
||||
|
||||
simple_thread_pool::simple_thread_pool(size_t num_threads)
|
||||
{
|
||||
for (size_t i{0}; i < num_threads; ++i)
|
||||
{
|
||||
workers_.emplace_back(
|
||||
[this]()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
std::function<void()> job;
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(thread_mutex_);
|
||||
|
||||
cv_.wait(lck, [this]() -> bool { return shutdown_ || !task_queue_.empty(); });
|
||||
|
||||
if (shutdown_ && task_queue_.empty()) return;
|
||||
|
||||
job = std::move(task_queue_.front());
|
||||
task_queue_.pop();
|
||||
}
|
||||
|
||||
job();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
simple_thread_pool::~simple_thread_pool()
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(thread_mutex_);
|
||||
shutdown_ = true;
|
||||
}
|
||||
|
||||
cv_.notify_all();
|
||||
for (auto& th : workers_)
|
||||
{
|
||||
th.join();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace pixelarium::utils
|
||||
{
|
||||
class simple_thread_pool
|
||||
{
|
||||
public:
|
||||
explicit simple_thread_pool(size_t);
|
||||
simple_thread_pool(simple_thread_pool&) = delete;
|
||||
simple_thread_pool(const simple_thread_pool&) = delete;
|
||||
simple_thread_pool(simple_thread_pool&&) = delete;
|
||||
simple_thread_pool& operator=(simple_thread_pool&) = delete;
|
||||
simple_thread_pool& operator=(simple_thread_pool&&) = delete;
|
||||
~simple_thread_pool();
|
||||
|
||||
template <typename Callable>
|
||||
requires std::invocable<Callable>
|
||||
static auto run_asynch(Callable&& fun) -> void
|
||||
{
|
||||
simple_thread_pool::Global().enqueue(std::forward<Callable>(fun));
|
||||
}
|
||||
|
||||
public:
|
||||
template <typename Callable>
|
||||
requires std::invocable<Callable>
|
||||
auto enqueue(Callable&& fun) -> void
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(thread_mutex_);
|
||||
task_queue_.emplace(std::forward<Callable>(fun));
|
||||
}
|
||||
|
||||
cv_.notify_one();
|
||||
}
|
||||
|
||||
private:
|
||||
static auto Global() -> simple_thread_pool&
|
||||
{
|
||||
const auto kThreadCount{std::thread::hardware_concurrency() * 2};
|
||||
static simple_thread_pool global_instance(kThreadCount == 0 ? 5 : kThreadCount);
|
||||
return global_instance;
|
||||
}
|
||||
std::vector<std::thread> workers_;
|
||||
std::condition_variable cv_;
|
||||
std::mutex thread_mutex_;
|
||||
std::queue<std::function<void()>> task_queue_;
|
||||
bool shutdown_{false};
|
||||
};
|
||||
} // namespace pixelarium::utils
|
||||
Reference in New Issue
Block a user