Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

core.atomic

The atomic module provides basic support for lock-free concurrent programming.
Note: Use the-preview=nosharedaccess compiler flag to detect unsafe individual read or write operations on shared data.
License:
Boost License 1.0
Authors:
Sean Kelly, Alex Rønne Petersen, Manu Evans

Sourcecore/atomic.d

Examples:
int y = 2;sharedint x = y;// OK//x++; // read modify write errorx.atomicOp!"+="(1);// OK//y = x; // read error with preview flagy = x.atomicLoad();// OKassert(y == 3);//x = 5; // write error with preview flagx.atomicStore(5);// OKassert(x.atomicLoad() == 5);
enumMemoryOrder: int;
Specifies the memory ordering semantics of an atomic operation.
See Also:
raw
Not sequenced. Corresponds toLLVM AtomicOrdering.Monotonic and C++11/C11memory_order_relaxed.
acq
Hoist-load + hoist-store barrier. Corresponds toLLVM AtomicOrdering.Acquire and C++11/C11memory_order_acquire.
rel
Sink-load + sink-store barrier. Corresponds toLLVM AtomicOrdering.Release and C++11/C11memory_order_release.
acq_rel
Acquire + release barrier. Corresponds toLLVM AtomicOrdering.AcquireRelease and C++11/C11memory_order_acq_rel.
seq
Fully sequenced (acquire + release). Corresponds toLLVM AtomicOrdering.SequentiallyConsistent and C++11/C11memory_order_seq_cst.
pure nothrow @nogc @trusted TatomicLoad(MemoryOrder ms = MemoryOrder.seq, T)(auto ref return scope const Tval)
if (!is(T == shared(U), U) && !is(T == shared(inout(U)), U) && !is(T == shared(const(U)), U));

pure nothrow @nogc @trusted TatomicLoad(MemoryOrder ms = MemoryOrder.seq, T)(auto ref return scope const shared Tval)
if (!hasUnsharedIndirections!T);

pure nothrow @nogc @trusted TailShared!TatomicLoad(MemoryOrder ms = MemoryOrder.seq, T)(auto ref const shared Tval)
if (hasUnsharedIndirections!T);
Loads 'val' from memory and returns it. The memory barrier specified by 'ms' is applied to the operation, which is fully sequenced by default. Valid memory orders are MemoryOrder.raw, MemoryOrder.acq, and MemoryOrder.seq.
Parameters:
TvalThe target variable.
Returns:
The value of 'val'.
pure nothrow @nogc @trusted voidatomicStore(MemoryOrder ms = MemoryOrder.seq, T, V)(ref Tval, Vnewval)
if (!is(T == shared) && !is(V == shared));

pure nothrow @nogc @trusted voidatomicStore(MemoryOrder ms = MemoryOrder.seq, T, V)(ref shared Tval, Vnewval)
if (!is(T == class));

pure nothrow @nogc @trusted voidatomicStore(MemoryOrder ms = MemoryOrder.seq, T, V)(ref shared Tval, auto ref shared Vnewval)
if (is(T == class));
Writes 'newval' into 'val'. The memory barrier specified by 'ms' is applied to the operation, which is fully sequenced by default. Valid memory orders are MemoryOrder.raw, MemoryOrder.rel, and MemoryOrder.seq.
Parameters:
TvalThe target variable.
VnewvalThe value to store.
pure nothrow @nogc @trusted TatomicFetchAdd(MemoryOrder ms = MemoryOrder.seq, T)(ref return scope Tval, size_tmod)
if ((__traits(isIntegral, T) || is(T == U*, U)) && !is(T == shared));

pure nothrow @nogc @trusted TatomicFetchAdd(MemoryOrder ms = MemoryOrder.seq, T)(ref return scope shared Tval, size_tmod)
if (__traits(isIntegral, T) || is(T == U*, U));
Atomically addsmod to the value referenced byval and returns the valueval held previously. This operation is both lock-free and atomic.
Parameters:
TvalReference to the value to modify.
size_tmodThe value to add.
Returns:
The value held previously byval.
pure nothrow @nogc @trusted TatomicFetchSub(MemoryOrder ms = MemoryOrder.seq, T)(ref return scope Tval, size_tmod)
if ((__traits(isIntegral, T) || is(T == U*, U)) && !is(T == shared));

pure nothrow @nogc @trusted TatomicFetchSub(MemoryOrder ms = MemoryOrder.seq, T)(ref return scope shared Tval, size_tmod)
if (__traits(isIntegral, T) || is(T == U*, U));
Atomically subtractsmod from the value referenced byval and returns the valueval held previously. This operation is both lock-free and atomic.
Parameters:
TvalReference to the value to modify.
size_tmodThe value to subtract.
Returns:
The value held previously byval.
pure nothrow @nogc @trusted TatomicExchange(MemoryOrder ms = MemoryOrder.seq, T, V)(T*here, VexchangeWith)
if (!is(T == shared) && !is(V == shared));

pure nothrow @nogc @trusted TailShared!TatomicExchange(MemoryOrder ms = MemoryOrder.seq, T, V)(shared(T)*here, VexchangeWith)
if (!is(T == class) && !is(T == interface));

pure nothrow @nogc @trusted shared(T)atomicExchange(MemoryOrder ms = MemoryOrder.seq, T, V)(shared(T)*here, shared(V)exchangeWith)
if (is(T == class) || is(T == interface));
ExchangeexchangeWith with the memory referenced byhere. This operation is both lock-free and atomic.
Parameters:
T*hereThe address of the destination variable.
VexchangeWithThe value to exchange.
Returns:
The value held previously byhere.
templatecas(MemoryOrder succ = MemoryOrder.seq, MemoryOrder fail = MemoryOrder.seq)
Performs either compare-and-set or compare-and-swap (or exchange).
There are two categories of overloads in this template: The first category does a simple compare-and-set. The comparison value (ifThis) is treated as an rvalue.
The second category does a compare-and-swap (a.k.a. compare-and-exchange), and expectsifThis to be a pointer type, where the previous value ofhere will be written.
This operation is both lock-free and atomic.
Parameters:
T* hereThe address of the destination variable.
V2 writeThisThe value to store.
V1 ifThisThe comparison value.
Returns:
true if the store occurred, false if not.
pure nothrow @nogc @trusted boolcas(T, V1, V2)(T*here, V1ifThis, V2writeThis)
if (!is(T == shared) && is(T : V1));
Compare-and-set for non-shared values
pure nothrow @nogc @trusted boolcas(T, V1, V2)(shared(T)*here, V1ifThis, V2writeThis)
if (!is(T == class) && (is(T : V1) || is(shared(T) : V1)));
Compare-and-set for shared value type
pure nothrow @nogc @trusted boolcas(T, V1, V2)(shared(T)*here, shared(V1)ifThis, shared(V2)writeThis)
if (is(T == class));
Compare-and-set forshared reference type (class)
pure nothrow @nogc @trusted boolcas(T, V)(T*here, T*ifThis, VwriteThis)
if (!is(T == shared) && !is(V == shared));
Compare-and-exchange for non-shared types
pure nothrow @nogc @trusted boolcas(T, V1, V2)(shared(T)*here, V1*ifThis, V2writeThis)
if (!is(T == class) && (is(T : V1) || is(shared(T) : V1)));
Compare and exchange for mixed-sharedness types
pure nothrow @nogc @trusted boolcas(T, V)(shared(T)*here, shared(T)*ifThis, shared(V)writeThis)
if (is(T == class));
Compare-and-exchange forclass
pure nothrow @nogc @trusted boolcasWeak(MemoryOrder succ = MemoryOrder.seq, MemoryOrder fail = MemoryOrder.seq, T, V1, V2)(T*here, V1ifThis, V2writeThis)
if (!is(T == shared) && is(T : V1));

pure nothrow @nogc @trusted boolcasWeak(MemoryOrder succ = MemoryOrder.seq, MemoryOrder fail = MemoryOrder.seq, T, V1, V2)(shared(T)*here, V1ifThis, V2writeThis)
if (!is(T == class) && (is(T : V1) || is(shared(T) : V1)));

pure nothrow @nogc @trusted boolcasWeak(MemoryOrder succ = MemoryOrder.seq, MemoryOrder fail = MemoryOrder.seq, T, V1, V2)(shared(T)*here, shared(V1)ifThis, shared(V2)writeThis)
if (is(T == class));
Stores 'writeThis' to the memory referenced by 'here' if the value referenced by 'here' is equal to 'ifThis'. The 'weak' version of cas may spuriously fail. It is recommended to usecasWeak only whencas would be used in a loop. This operation is both lock-free and atomic.
Parameters:
T*hereThe address of the destination variable.
V2writeThisThe value to store.
V1ifThisThe comparison value.
Returns:
true if the store occurred, false if not.
pure nothrow @nogc @trusted boolcasWeak(MemoryOrder succ = MemoryOrder.seq, MemoryOrder fail = MemoryOrder.seq, T, V)(T*here, T*ifThis, VwriteThis)
if (!is(T == shared(S), S) && !is(V == shared(U), U));

pure nothrow @nogc @trusted boolcasWeak(MemoryOrder succ = MemoryOrder.seq, MemoryOrder fail = MemoryOrder.seq, T, V1, V2)(shared(T)*here, V1*ifThis, V2writeThis)
if (!is(T == class) && (is(T : V1) || is(shared(T) : V1)));

pure nothrow @nogc @trusted boolcasWeak(MemoryOrder succ = MemoryOrder.seq, MemoryOrder fail = MemoryOrder.seq, T, V)(shared(T)*here, shared(T)*ifThis, shared(V)writeThis)
if (is(T == class));
Stores 'writeThis' to the memory referenced by 'here' if the value referenced by 'here' is equal to the value referenced by 'ifThis'. The prior value referenced by 'here' is written toifThis and returned to the user. The 'weak' version of cas may spuriously fail. It is recommended to usecasWeak only whencas would be used in a loop. This operation is both lock-free and atomic.
Parameters:
T*hereThe address of the destination variable.
VwriteThisThe value to store.
T*ifThisThe address of the value to compare, and receives the prior value ofhere as output.
Returns:
true if the store occurred, false if not.
pure nothrow @nogc @safe voidatomicFence(MemoryOrder order = MemoryOrder.seq)();
Inserts a full load/store memory fence (on platforms that need it). This ensures that all loads and stores before a call to this function are executed before any loads and stores after the call.
pure nothrow @nogc @safe voidpause();
Gives a hint to the processor that the calling thread is in a 'spin-wait' loop, allowing to more efficiently allocate resources.
pure nothrow @nogc @safe TailShared!TatomicOp(string op, T, V1)(ref shared Tval, V1mod)
if (__traits(compiles, mixin("*cast(T*)&val" ~ op ~ "mod")));
Performs the binary operation 'op' on val using 'mod' as the modifier.
Parameters:
TvalThe target variable.
V1modThe modifier to apply.
Returns:
The result of the operation.
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Fri Feb 20 17:56:51 2026

[8]ページ先頭

©2009-2026 Movatter.jp