4 ms·
Honest question: why would atomics be necessary or useful if data isn’t shared between threads?
by cdogl 3y ago
Honest question: why would atomics be necessary or useful if data isn’t shared between threads?
- NL807 3y agoBecause at some point data has to be exchanged across threads. For example a task queue might have tasks that can independently executed in a thread pool, but the queue index has to be atomically modified when some other thread emplaced a new task. Or if you want to transfer ownership of a heap allocated object between threads, you need to atomically transfer the pointer, or modify the reference count of that pointer. Things like that.
- tormeh 3y agoYou can and should use atomics, just not in any kind of hot loop. Using atomics is fine but expensive.
- loeg 3y agoYou can reduce sharing probabilistically, for example -- because contention is an N-squared problem, reducing sharing by some linear factor is enough for a large reduction in contention. You aren't eliminating contested atomics entirely, just making them low-contention rather than highly contended.