QtBase
v6.3.1
src
corelib
doc
snippets
code
src_corelib_thread_qsemaphore.cpp
Go to the documentation of this file.
1
/****************************************************************************
2
**
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
5
**
6
** This file is part of the documentation of the Qt Toolkit.
7
**
8
** $QT_BEGIN_LICENSE:BSD$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and The Qt Company. For licensing terms
14
** and conditions see https://www.qt.io/terms-conditions. For further
15
** information use the contact form at https://www.qt.io/contact-us.
16
**
17
** BSD License Usage
18
** Alternatively, you may use this file under the terms of the BSD license
19
** as follows:
20
**
21
** "Redistribution and use in source and binary forms, with or without
22
** modification, are permitted provided that the following conditions are
23
** met:
24
** * Redistributions of source code must retain the above copyright
25
** notice, this list of conditions and the following disclaimer.
26
** * Redistributions in binary form must reproduce the above copyright
27
** notice, this list of conditions and the following disclaimer in
28
** the documentation and/or other materials provided with the
29
** distribution.
30
** * Neither the name of The Qt Company Ltd nor the names of its
31
** contributors may be used to endorse or promote products derived
32
** from this software without specific prior written permission.
33
**
34
**
35
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46
**
47
** $QT_END_LICENSE$
48
**
49
****************************************************************************/
50
52
QSemaphore
sem
(5);
// sem.available() == 5
53
54
sem
.
acquire
(3);
// sem.available() == 2
55
sem
.
acquire
(2);
// sem.available() == 0
56
sem
.
release
(5);
// sem.available() == 5
57
sem
.
release
(5);
// sem.available() == 10
58
59
sem
.
tryAcquire
(1);
// sem.available() == 9, returns true
60
sem
.
tryAcquire
(250);
// sem.available() == 9, returns false
62
63
65
QSemaphore
sem
(5);
// a semaphore that guards 5 resources
66
sem
.
acquire
(5);
// acquire all 5 resources
67
sem
.
release
(5);
// release the 5 resources
68
sem
.
release
(10);
// "create" 10 new resources
70
71
73
QSemaphore
sem
(5);
// sem.available() == 5
74
sem
.
tryAcquire
(250);
// sem.available() == 5, returns false
75
sem
.
tryAcquire
(3);
// sem.available() == 2, returns true
77
78
80
QSemaphore
sem
(5);
// sem.available() == 5
81
sem
.
tryAcquire
(250, 1000);
// sem.available() == 5, waits 1000 milliseconds and returns false
82
sem
.
tryAcquire
(3, 30000);
// sem.available() == 2, returns true without waiting
84
86
// ... do something that may throw or return early
87
sem
.
release
();
89
91
const
QSemaphoreReleaser
releaser
(
sem
);
92
// ... do something that may throw or early return
93
// implicitly calls sem.release() here and at every other return in between
95
97
{
// some scope
98
QSemaphoreReleaser
releaser
;
// does nothing
99
// ...
100
if
(someCondition) {
101
releaser
=
QSemaphoreReleaser
(
sem
);
102
// ...
103
}
104
// ...
105
}
// conditionally calls sem.release(), depending on someCondition
107
109
releaser
.
cancel
();
// avoid releasing old semaphore()
110
releaser
=
QSemaphoreReleaser
(
sem
, 42);
111
// now will call sem.release(42) when 'releaser' is destroyed
QSemaphore
The QSemaphore class provides a general counting semaphore.
Definition:
qsemaphore.h:55
QSemaphore::acquire
void acquire(int n=1)
Definition:
qsemaphore.cpp:323
QSemaphore::tryAcquire
bool tryAcquire(int n=1)
Definition:
qsemaphore.cpp:438
QSemaphore::release
void release(int n=1)
Definition:
qsemaphore.cpp:351
QSemaphoreReleaser
The QSemaphoreReleaser class provides exception-safe deferral of a QSemaphore::release() call.
Definition:
qsemaphore.h:93
QSemaphoreReleaser::cancel
QSemaphore * cancel() noexcept
Definition:
qsemaphore.h:119
releaser
const QSemaphoreReleaser releaser(sem)
[4]
Definition:
src_corelib_thread_qsemaphore.cpp:110
sem
QSemaphore sem(5)
[0]
Generated by
1.9.1