Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
42//! [7]
44
45void cacheObject(const QString &key, SomeClass *object)
46//! [7] //! [8]
47{
48 caches.localData().insert(key, object);
49}
50
51void removeFromCache(const QString &key)
52//! [8] //! [9]
53{
54 if (!caches.hasLocalData())
55 return;
56
57 caches.localData().remove(key);
58}
59//! [9]
60
61int main()
62{
63 return 0;
64}
void increment()
Definition threads.cpp:15
void decrement()
Definition threads.cpp:16
int value() const
Definition threads.cpp:17
int main()
[open]
#define Counter
[0]
QThreadStorage< QCache< QString, SomeClass > > caches
[7]
Definition threads.cpp:43
int SomeClass
Definition threads.cpp:40
void removeFromCache(const QString &key)
[8] //! [9]
Definition threads.cpp:51
void cacheObject(const QString &key, SomeClass *object)
[7] //! [8]
Definition threads.cpp:45