Variable modified by different threads.

Ali Çehreli acehreli at yahoo.com
Mon Dec 2 19:12:00 UTC 2024


On 12/2/24 7:18 AM, Salih Dincer wrote:
 > On Monday, 2 December 2024 at 08:00:40 UTC, Richard (Rikki) Andrew
 > Cattermole wrote:
 >> You don't need both atomics an mutex's, pick one.
 >
 > The compiler wants us to use atomicOp; If you want, take it out of the
 > synchronized(mutex) { } block, it doesn't matter:

Then use atomicOp. You don't need any mutex for this problem, especially 
when they can make this program very slow. Imagine how a thread will 
have to relinquish its execution time to wait for a mutex in order to 
increment a simple int. The CPU's atomic operation primitives already 
achieve that.

Another general reason for avoiding a mutex is that they are low level 
primitives, which should be needed only in special cases when existing 
solution that are already based on them don't work for your problem for 
some reason. For example, std.parallelism and std.concurrency already 
use features like mutexes but perhaps they can't be used for some reason.

Ali



More information about the Digitalmars-d-learn mailing list