digiKam
Loading...
Searching...
No Matches
recognitiontrainingupdatequeue.h
Go to the documentation of this file.
1/* ============================================================
2 *
3 * This file is a part of digiKam
4 *
5 * Date : 2024-10-28
6 * Description : Threadsafe queue for submitting face training removal requests.
7 *
8 * SPDX-FileCopyrightText: 2024 by Michael Miller <micahel underscore miller at msn dot com>
9 * SPDX-FileCopyrightText: 2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
10 *
11 * SPDX-License-Identifier: GPL-2.0-or-later
12 *
13 * ============================================================ */
14
15#pragma once
16
17// C++ includes
18
19#include <queue>
20#include <mutex>
21#include <condition_variable>
22
23// Qt includes
24
25#include <QString>
26#include <QThread>
27
28// Local includes
29
30#include "digikam_export.h"
31
32namespace Digikam
33{
34
35/*
36template <typename T>
37class SharedQueue
38{
39public:
40
41 SharedQueue();
42 ~SharedQueue();
43
44 T& front();
45 void pop_front();
46
47 void push_back(T& item);
48 void push_back(T&& item);
49
50 int size();
51 bool empty();
52
53private:
54
55 std::deque<T> queue_;
56 std::mutex mutex_;
57 std::condition_variable cond_;
58};
59
60template <typename T>
61SharedQueue<T>::SharedQueue() {}
62
63template <typename T>
64SharedQueue<T>::~SharedQueue() {}
65
66template <typename T>
67T& SharedQueue<T>::front()
68{
69 std::unique_lock<std::mutex> mlock(mutex_);
70
71 while (queue_.empty())
72 {
73 cond_.wait(mlock);
74 }
75
76 return queue_.front();
77}
78
79template <typename T>
80void SharedQueue<T>::pop_front()
81{
82 std::unique_lock<std::mutex> mlock(mutex_);
83
84 while (queue_.empty())
85 {
86 cond_.wait(mlock);
87 }
88
89 queue_.pop_front();
90}
91
92template <typename T>
93void SharedQueue<T>::push_back(T& item)
94{
95 std::unique_lock<std::mutex> mlock(mutex_);
96 queue_.push_back(item);
97 mlock.unlock(); // Unlock before notificiation to minimize mutex con.
98 cond_.notify_one(); // Notify one waiting thread.
99}
100
101template <typename T>
102void SharedQueue<T>::push_back(T&& item)
103{
104 std::unique_lock<std::mutex> mlock(mutex_);
105 queue_.push_back(std::move(item));
106 mlock.unlock(); // Unlock before notificiation to minimize mutex con.
107 cond_.notify_one(); // Notify one waiting thread?
108}
109
110template <typename T>
111int SharedQueue<T>::size()
112{
113 std::unique_lock<std::mutex> mlock(mutex_);
114 int size = queue_.size();
115 mlock.unlock();
116
117 return size;
118}
119*/
120
121// -----------------------------------------------------
122
123template <typename T>
124class SharedQueue;
125
127{
128public:
129
132
133public:
134
135 void push(const QString& hash);
136 void pop();
137 QString front();
138
139 QString endSignal();
140
141 void registerReaderThread(const QThread* thread);
142 void unregisterReaderThread(const QThread* thread);
143
144private:
145
146 static SharedQueue<QString> queue;
147 static QList<const QThread*> readers;
148 static int ref;
149
150 Q_DISABLE_COPY(RecognitionTrainingUpdateQueue)
151};
152
153} // namespace Digikam
Definition recognitiontrainingupdatequeue.h:127
Definition datefolderview.cpp:34