TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkconcurrentqueue.h
Go to the documentation of this file.
1 #ifndef TTKCONCURRENTQUEUE_H
2 #define TTKCONCURRENTQUEUE_H
3 
4 /***************************************************************************
5  * This file is part of the TTK Library Module project
6  * Copyright (C) 2015 - 2024 Greedysky Studio
7 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17 
18  * You should have received a copy of the GNU Lesser General Public License along
19  * with this program; If not, see <http://www.gnu.org/licenses/>.
20  ***************************************************************************/
21 
22 #include <queue>
23 #include <condition_variable>
24 
28 template <typename T>
30 {
31 public:
36  : m_queue(),
37  m_mutex(),
38  m_condition()
39  {
40 
41  }
42 
46  inline void push(const T &record)
47  {
48  std::lock_guard<std::mutex> lock(m_mutex);
49  m_queue.push(record);
50  m_condition.notify_one();
51  }
52 
56  inline bool pop(T &record, bool is_blocked = true)
57  {
58  // If user wants to retrieve data in non-blocking mode
59  if(is_blocked)
60  {
61  std::unique_lock<std::mutex> lock(m_mutex);
62  while(m_queue.empty())
63  {
64  m_condition.wait(lock);
65  }
66  }
67  else
68  {
69  std::lock_guard<std::mutex> lock(m_mutex);
70  if(m_queue.empty())
71  {
72  return false;
73  }
74  }
75 
76  record = std::move(m_queue.front());
77  m_queue.pop();
78  return true;
79  }
80 
84  inline size_t size() const
85  {
86  std::lock_guard<std::mutex> lock(m_mutex);
87  return m_queue.size();
88  }
89 
93  inline bool empty() const
94  {
95  std::lock_guard<std::mutex> lock(m_mutex);
96  return m_queue.empty();
97  }
98 
102  inline void clear()
103  {
104  std::queue<T> empty;
105  std::swap(empty, m_queue);
106  }
107 
108 private:
109  std::queue<T> m_queue;
110  mutable std::mutex m_mutex;
111  std::condition_variable m_condition;
112 
113 };
114 
115 #endif // TTKCONCURRENTQUEUE_H
#define T(v)
Definition: http_parser.c:237
std::queue< T > m_queue
The class of the concurrent queue.
void swap(TTKAny &left, TTKAny &right) noexcept
Definition: ttkany.h:212
void push(const T &record)
std::condition_variable m_condition
bool pop(T &record, bool is_blocked=true)
size_t size() const