6 void LoaderThread::start(
const std::shared_ptr<void> &owner,
7 const std::function<
void()> &fnc) {
8 std::unique_lock<std::mutex> lock(_mutex);
9 _queue.emplace_back(owner, fnc);
10 _condition.notify_one();
13 void LoaderThread::cancel(
const std::shared_ptr<void> &owner) {
14 std::unique_lock<std::mutex> lock(_mutex);
15 for (
auto it = _queue.begin(); it != _queue.end();) {
16 if (it->first == owner) {
17 it = _queue.erase(it);
24 LoaderThread::LoaderThread() {
26 _thread = std::thread([
this]() {
28 std::function<void()> job;
30 std::unique_lock<std::mutex> lock(_mutex);
35 if (!_queue.empty()) {
36 _count = _queue.size();
37 job = _queue.front().second;
42 _condition.wait(lock);
52 LoaderThread::~LoaderThread() {
54 std::unique_lock<std::mutex> lock(_mutex);
56 _condition.notify_all();
61 const std::shared_ptr<LoaderThread> &LoaderThread::instance() {
62 static auto instance = std::make_shared<LoaderThread>();