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
src_corelib_kernel_qdeadlinetimer.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QDeadlineTimer>
5#include <QMutex>
6
7void waitForReadyRead(QDeadlineTimer &deadline);
8bool readFromDevice(int msecs) { return false; }
9void cleanup();
10
12{
13public:
14 bool waitForReadyRead(QDeadlineTimer &deadline)
15 {
16 return deadline.remainingTime() > 0;
17 }
18};
19
20//! [0]
21 void executeOperation(int msecs)
22 {
23 QDeadlineTimer deadline(msecs);
24 do {
25 if (readFromDevice(deadline.remainingTime()))
26 break;
27 waitForReadyRead(deadline);
28 } while (!deadline.hasExpired());
29 }
30//! [0]
31
32
33bool examples(MyDevice *device, QMutex &mutex)
34{
35 {
36 //! [1]
37 using namespace std::chrono;
38 using namespace std::chrono_literals;
39
40 QDeadlineTimer deadline(30s);
41 device->waitForReadyRead(deadline);
42 if (deadline.remainingTimeAsDuration() > 300ms)
44 //! [1]
45 }
46
47 {
48 //! [2]
49 using namespace std::chrono;
50 using namespace std::chrono_literals;
51 auto now = steady_clock::now();
52 QDeadlineTimer deadline(now + 1s);
53 Q_ASSERT(deadline == now + 1s);
54 //! [2]
55 }
56
57 {
58 //! [3]
59 using namespace std::chrono_literals;
60 QDeadlineTimer deadline(250ms);
61 //! [3]
62 }
63
64 QDeadlineTimer deadline;
65
66 {
67 //! [4]
68 using namespace std::chrono_literals;
69 deadline.setRemainingTime(250ms);
70 //! [4]
71 }
72
73 {
74 //! [5]
75 mutex.tryLock(deadline.remainingTime());
76 //! [5]
77 }
78
79 {
80 //! [6]
81 qint64 realTimeLeft = deadline.deadline();
82 if (realTimeLeft != (std::numeric_limits<qint64>::max)()) {
83 realTimeLeft -= QDeadlineTimer::current().deadline();
84 // or:
85 //QElapsedTimer timer;
86 //timer.start();
87 //realTimeLeft -= timer.msecsSinceReference();
88 }
89 //! [6]
90 }
91
92 {
93 //! [7]
94 qint64 realTimeLeft = deadline.deadlineNSecs();
95 if (realTimeLeft != std::numeric_limits<qint64>::max())
96 realTimeLeft -= QDeadlineTimer::current().deadlineNSecs();
97 //! [7]
98 }
99
100 QDeadlineTimer lhs(1);
101 QDeadlineTimer rhs(2);
102
103 {
104 //! [8]
105 return lhs.deadlineNSecs() == rhs.deadlineNSecs();
106 //! [8]
107 }
108
109 {
110 //! [9]
111 return lhs.deadlineNSecs() != rhs.deadlineNSecs();
112 //! [9]
113 }
114
115 {
116 //! [10]
117 return lhs.deadlineNSecs() < rhs.deadlineNSecs();
118 //! [10]
119 }
120
121 {
122 //! [11]
123 return lhs.deadlineNSecs() <= rhs.deadlineNSecs();
124 //! [11]
125 }
126
127 {
128 //! [12]
129 return lhs.deadlineNSecs() > rhs.deadlineNSecs();
130 //! [12]
131 }
132
133 {
134 //! [13]
135 return lhs.deadlineNSecs() >= rhs.deadlineNSecs();
136 //! [13]
137 }
138}
bool waitForReadyRead(QDeadlineTimer &deadline)
bool readFromDevice(int msecs)
void cleanup()
void waitForReadyRead(QDeadlineTimer &deadline)
void executeOperation(int msecs)
[0]
bool examples(MyDevice *device, QMutex &mutex)
[0]