Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
10
class
Counter
11
{
12
public
:
13
Counter
() { n = 0; }
14
15
void
increment
() { ++n; }
16
void
decrement
() { --n; }
17
int
value
()
const
{
return
n; }
18
19
private
:
20
int
n;
21
};
22
23
#
undef
Counter
24
#
define
Counter
ThreadSafeCounter
25
26
class
Counter
27
{
28
public
:
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
35
private
:
36
mutable
QMutex mutex;
37
int
n;
38
};
39
40
typedef
int
SomeClass
;
41
42
//! [7]
43
QThreadStorage
<
QCache
<
QString
,
SomeClass
> >
caches
;
44
45
void
cacheObject
(
const
QString &key,
SomeClass
*object)
46
//! [7] //! [8]
47
{
48
caches
.localData().insert(key, object);
49
}
50
51
void
removeFromCache
(
const
QString &key)
52
//! [8] //! [9]
53
{
54
if
(!caches.hasLocalData())
55
return
;
56
57
caches
.localData().remove(key);
58
}
59
//! [9]
60
61
int
main
()
62
{
63
return
0;
64
}
Counter::increment
void increment()
Definition
threads.cpp:15
Counter::decrement
void decrement()
Definition
threads.cpp:16
Counter::value
int value() const
Definition
threads.cpp:17
main
int main()
[open]
Definition
doc_src_objecttrees.cpp:7
Counter
#define Counter
[0]
Definition
signalsandslots.h:7
caches
QThreadStorage< QCache< QString, SomeClass > > caches
[7]
Definition
threads.cpp:43
SomeClass
int SomeClass
Definition
threads.cpp:40
removeFromCache
void removeFromCache(const QString &key)
[8] //! [9]
Definition
threads.cpp:51
cacheObject
void cacheObject(const QString &key, SomeClass *object)
[7] //! [8]
Definition
threads.cpp:45
qtbase
src
corelib
doc
snippets
threads
threads.cpp
Generated on
for Qt by
1.18.0