Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
threads.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QCache>
5#include <QMutex>
6#include <QThreadStorage>
7
8#define Counter ReentrantCounter
9
10class Counter
11{
12public:
13 Counter() { n = 0; }
14
15 void increment() { ++n; }
16 void decrement() { --n; }
17 int value() const { return n; }
18
19private:
20 int n;
21};
22
23#undef Counter
24#define Counter ThreadSafeCounter
25
26class Counter
27{
28public:
29 Counter() { n = 0; }
30
31 void increment() { QMutexLocker locker(&mutex); ++n; }
32 void decrement() { QMutexLocker locker(&mutex); --n; }
33 int value() const { QMutexLocker locker(&mutex); return n; }
34
35private:
36 mutable QMutex mutex;
37 int n;
38};
39
40typedef int SomeClass;
41
43QThreadStorage<QCache<QString, SomeClass> > caches;
44
45void cacheObject(const QString &key, SomeClass *object)
47{
48 caches.localData().insert(key, object);
49}
50
53{
54 if (!caches.hasLocalData())
55 return;
56
57 caches.localData().remove(key);
58}
60
61int main()
62{
63 return 0;
64}
Counter()
Definition threads.cpp:13
void increment()
Definition threads.cpp:15
void decrement()
Definition threads.cpp:16
int value() const
Definition threads.cpp:17
\inmodule QtCore
Definition qmutex.h:313
\inmodule QtCore
Definition qmutex.h:281
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
GLuint64 key
GLfloat n
QMutex mutex
[2]
int SomeClass
Definition threads.cpp:40
void removeFromCache(const QString &key)
[8] //! [9]
Definition threads.cpp:51
QThreadStorage< QCache< QString, SomeClass > > caches
[7]
Definition threads.cpp:43
void cacheObject(const QString &key, SomeClass *object)
[7] //! [8]
Definition threads.cpp:45
int main()
[9]
Definition threads.cpp:61