digitalmars.D.learn - difference between x.atomicOp!"+="(1) and atomicFetchAdd(x, 1)?
- mw (18/18) Nov 10 2022 Hi,
- Paul Backus (4/7) Nov 10 2022 Looking at the source in druntime, `atomicOp!"+="` forwards to
- mw (8/16) Nov 10 2022 Thanks! Indeed:
Hi, Anyone can help explain what is the difference between x.atomicOp!"+="(1) and atomicFetchAdd(x, 1)? From the doc, their return values are different atomicOp Performs the binary operation 'op' on val using 'mod' as the modifier. Returns: The result of the operation. atomicFetchAdd Atomically adds mod to the value referenced by val and returns the value val held previously. This operation is both lock-free and atomic. Returns: The value held previously by val. Apart from this, any other difference, esp in a multithreaded program? Are they the same? Is atomicOp also lock-free? Thanks.
Nov 10 2022
On Thursday, 10 November 2022 at 17:04:31 UTC, mw wrote:Hi, Anyone can help explain what is the difference between x.atomicOp!"+="(1) and atomicFetchAdd(x, 1)?Looking at the source in druntime, `atomicOp!"+="` forwards to `atomicFetchAdd` internally, so they should have the same behavior.
Nov 10 2022
On Thursday, 10 November 2022 at 18:30:16 UTC, Paul Backus wrote:On Thursday, 10 November 2022 at 17:04:31 UTC, mw wrote:Thanks! Indeed: https://github.com/dlang/dmd/blob/master/druntime/src/core/atomic.d#L582 (source is always your best friend :-) and looks like atomicFetchAdd is more fundamental: ``` return cast(T)(atomicFetchAdd(val, mod) + mod); ```Hi, Anyone can help explain what is the difference between x.atomicOp!"+="(1) and atomicFetchAdd(x, 1)?Looking at the source in druntime, `atomicOp!"+="` forwards to `atomicFetchAdd` internally, so they should have the same behavior.
Nov 10 2022