TTKMusicPlayer  4.3.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkconcurrentdata.h
Go to the documentation of this file.
1 #ifndef TTKCONCURRENTDATA_H
2 #define TTKCONCURRENTDATA_H
3 
4 /***************************************************************************
5  * This file is part of the TTK Library Module project
6  * Copyright (C) 2015 - 2026 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 <mutex>
23 
27 template <typename T>
29 {
30 public:
34  TTKConcurrentData() noexcept
35  : m_data(),
36  m_mutex()
37  {
38 
39  }
40 
41  TTKConcurrentData(const T &v) noexcept
42  : m_data(v),
43  m_mutex()
44  {
45 
46  }
47 
49  : m_data(v.m_data),
50  m_mutex(v.m_mutex)
51  {
52 
53  }
54 
58  bool setUnblockedData(const T &value)
59  {
60  std::unique_lock<std::mutex> lock(m_mutex);
61  if(!lock.try_lock())
62  {
63  return false;
64  }
65 
66  m_data = value;
67  return true;
68  }
69 
73  bool unblockedData(T &value)
74  {
75  std::unique_lock<std::mutex> lock(m_mutex);
76  if(!lock.try_lock())
77  {
78  return false;
79  }
80 
81  value = m_data;
82  return true;
83  }
84 
88  void setBlockedData(const T &value)
89  {
90  std::lock_guard<std::mutex> lock(m_mutex);
91  m_data = value;
92  }
93 
97  T blockedData() const
98  {
99  std::lock_guard<std::mutex> lock(m_mutex);
100  return m_data;
101  }
102 
106  T* raw()
107  {
108  return &m_data;
109  }
110 
114  std::mutex& mutex()
115  {
116  return m_mutex;
117  }
118 
119 private:
121  std::mutex m_mutex;
122 
123 };
124 
125 #endif // TTKCONCURRENTDATA_H
#define T(v)
Definition: http_parser.c:237
std::mutex & mutex()
TTKConcurrentData() noexcept
TTKConcurrentData(const TTKConcurrentData &v) noexcept
TTKConcurrentData(const T &v) noexcept
bool setUnblockedData(const T &value)
The class of the concurrent data.
bool unblockedData(T &value)
void setBlockedData(const T &value)