summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets/code/src_corelib_thread_qatomic.cpp
blob: 2d55e6b706e812b323371921148af37f6d4e3790 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

//! [0]
MySharedType &MySharedType::operator=(const MySharedType &other)
{
    (void) other.data->atomicInt.ref();
    if (!data->atomicInt.deref()) {
        // The last reference has been released
        delete d;
    }
    d = other.d;
    return *this;
}
//! [0]


//! [1]
if (currentValue == expectedValue) {
    currentValue = newValue;
    return true;
}
return false;
//! [1]


//! [2]
int originalValue = currentValue;
currentValue = newValue;
return originalValue;
//! [2]


//! [3]
int originalValue = currentValue;
currentValue += valueToAdd;
return originalValue;
//! [3]


//! [4]
if (currentValue == expectedValue) {
    currentValue = newValue;
    return true;
}
return false;
//! [4]


//! [5]
T *originalValue = currentValue;
currentValue = newValue;
return originalValue;
//! [5]


//! [6]
T *originalValue = currentValue;
currentValue += valueToAdd;
return originalValue;
//! [6]