提供 QScopedValueRollback for atomic variables. 更多...
头: | #include <QAtomicScopedValueRollback> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
Since: | Qt 6.7 |
QAtomicScopedValueRollback (std::atomic<T> & var , std::memory_order mo = std::memory_order_seq_cst) | |
QAtomicScopedValueRollback (std::atomic<T> & var , T value , std::memory_order mo = std::memory_order_seq_cst) | |
QAtomicScopedValueRollback (QBasicAtomicInteger<T> & var , std::memory_order mo = std::memory_order_seq_cst) | |
QAtomicScopedValueRollback (QBasicAtomicInteger<T> & var , T value , std::memory_order mo = std::memory_order_seq_cst) | |
QAtomicScopedValueRollback (QBasicAtomicPointer<std::remove_pointer_t<T>> & var , std::memory_order mo = std::memory_order_seq_cst) | |
QAtomicScopedValueRollback (QBasicAtomicPointer<std::remove_pointer_t<T>> & var , T value , std::memory_order mo = std::memory_order_seq_cst) | |
~QAtomicScopedValueRollback () | |
void | commit () |
The QAtomicScopedValueRollback class resets an atomic variable to its prior value on destruction. It can be used to revert state when an exception is thrown without the need to write try-catch blocks.
It can also be used to manage variables that are temporarily set, such as reentrancy guards. By using this class, the variable will be reset whether the function is exited normally, exited early by a return statement, or exited by an exception.
The class works on std::atomic and the Qt atomic classes: QBasicAtomicInteger, QAtomicInteger , QAtomicInt , QBasicAtomicPointer and QAtomicPointer .
The memory accesses to the atomic variable
var
are specified using the value of
mo
. The memory order follows this mapping:
Otherwise, the default memory order is sequential consistent ordering.
注意: You should never name the template arguments explicitly, but exclusively use Class Template Argument Deduction (CTAD) and let the compiler pick the template argument.
注意: There is a chance that other threads modify the variable too, which means you may lose updates performed by other threads between the call to the QAtomicScopedValueRollback constructor and commit () or between commit () and the destructor.
另请参阅 QScopedValueRollback .
[explicit constexpr]
QAtomicScopedValueRollback::
QAtomicScopedValueRollback
(
QBasicAtomicInteger
<
T
> &
var
,
std::memory_order
mo
= std::memory_order_seq_cst)
[explicit constexpr]
QAtomicScopedValueRollback::
QAtomicScopedValueRollback
(
QBasicAtomicPointer
<
std::remove_pointer_t
<
T
>> &
var
,
std::memory_order
mo
= std::memory_order_seq_cst)
[explicit constexpr]
QAtomicScopedValueRollback::
QAtomicScopedValueRollback
(
std::atomic
<
T
> &
var
,
std::memory_order
mo
= std::memory_order_seq_cst)
Records the value of var in order to restore it on destruction.
这相当于:
T old_value = var.load(mo); // And in the destructor: var.store(old_value, mo);
The
mo
adjustment for the load is described in the
Memory Order
章节。
[explicit constexpr]
QAtomicScopedValueRollback::
QAtomicScopedValueRollback
(
QBasicAtomicInteger
<
T
> &
var
,
T
value
,
std::memory_order
mo
= std::memory_order_seq_cst)
[explicit constexpr]
QAtomicScopedValueRollback::
QAtomicScopedValueRollback
(
QBasicAtomicPointer
<
std::remove_pointer_t
<
T
>> &
var
,
T
value
,
std::memory_order
mo
= std::memory_order_seq_cst)
[explicit constexpr]
QAtomicScopedValueRollback::
QAtomicScopedValueRollback
(
std::atomic
<
T
> &
var
,
T
value
,
std::memory_order
mo
= std::memory_order_seq_cst)
赋值 value to var and stores the prior value of var internally for reverting on destruction.
这相当于:
T old_value = var.exchange(new_value, mo); // And in the destructor: var.store(old_value, mo);
Restores the stored value that was current at construction time, or at the last call to commit (), to the managed variable.
这相当于:
// In the constructor: T old_value = var.load(mo); // or: T old_value = exchange(new_value, mo); var.store(old_value, mo);
Where
mo
is the same as the one initially passed to the constructor. See
Memory Order
for the meaning of
mo
.
Updates the stored value to the managed variable's current value, loaded with the same memory order as on construction.
This updated value will be restored on destruction, instead of the original prior value.
这相当于:
// Given constructor: T old_value = var.load(mo); old_value = var.load(mo); // referesh it // And, in the destructor: var.store(old_value, mo);
Where
mo
is the same as the one initially passed to the constructor. See
Memory Order
for the meaning of
mo
.