on GCD, memory barriers, and paranoid minds

hello,

i know i could be overly paranoid at times.. but

is this the case that i need to protect my global memory location (that i read from / write to) with read/write memory barriers even if i'm only accessing that location from a single serial DispatchQueue? considering the fact that GCD can pick a different thread to run operations on that queue, and a further fact that different threads can run on different cores and thus have different L1/L2 caches, so that the barrier unprotected write to a location in one code invocation (that runs on my serial queue, that happens to be bound to thread1/core1 at the time) might not yet be visible to a different invocation of my code that runs a bit later on the same serial GCD queue but it so happens that now it runs on thread2/core2, so that the barrier unprotected read from that global memory location returns some stale data?

is the answer any different if we consider a serial (maxConcurrentCount=1) OperationQueue instead of a serial dispatch queue?

finally, is the answer any different if we consider a single NSThread / pthread instead of a serial dispatch queue? can a single thread be bound to different cores during its lifetime? (e.g. work on one core, then sleep, then awake on a different core).

thank you.
Answered by DTS Engineer in 665532022

[do] i need to protect my global memory location … with read/write
memory barriers even if i'm only accessing that location from a single
serial DispatchQueue?

No.

is the answer any different if we consider a serial
(maxConcurrentCount=1) OperationQueue instead of a serial dispatch
queue?

No.

is the answer any different if we consider a single NSThread /
pthread instead of a serial dispatch queue?

No.

In general the system concurrency primitives insert whatever barriers necessary to support this sort of serialised access.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Accepted Answer

[do] i need to protect my global memory location … with read/write
memory barriers even if i'm only accessing that location from a single
serial DispatchQueue?

No.

is the answer any different if we consider a serial
(maxConcurrentCount=1) OperationQueue instead of a serial dispatch
queue?

No.

is the answer any different if we consider a single NSThread /
pthread instead of a serial dispatch queue?

No.

In general the system concurrency primitives insert whatever barriers necessary to support this sort of serialised access.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
on GCD, memory barriers, and paranoid minds
 
 
Q