On Mon, 17 Aug 2026 15:02:36 +0200
HÃ¥kon Bugge <[email protected]> wrote:

> Add a stress test for locking and atomic synchronization primitives.
> 
> The test maintains an array of elements containing counters
> initialized to zero. It creates four worker threads per online CPU. On
> each iteration, a worker chooses two elements at random, increments
> the first counter, and decrements the second. Each completed pair of
> updates therefore preserves the sum of all counters.
...

I think 1000 elements/locks/counters is far too many.
You are trying to hit contention so you want a relatively small number.
Possibly similar to the number of threads running the test, but maybe less.
Perhaps nearer the number of cpus.

Also you are only going to see an issue if the RMW of the 'counter += 1'
is split by the same RMW of a different thread.
That is pretty unlikely even without a lock.
It would be better to force a short delay between the R and W.

If you use change the MX_ATOMIC_ADD to use the atomic_long functions
(I've forgotten the exact name) then all the counter are the same type
and can be removed from the union.
The default 'just use +=' code can then be moved to the bottom of mx_add().

David

Reply via email to