Nice. Similar pattern for heavily threaded code accessing shared state - split the variable into an array & ensure there’s a cacheline of space for each to avoid false sharing..
This is very similar to the pattern of putting counters in “thread-local storage” in a PC app; each thread increments its own private counter (which won’t be contended by anyone else), and it’s the reader’s responsibility to grab a coherent view of all of the counters and add them up. As long as you expect reads to be rare compared to increments, it works well.
Hey Sam, welcome to lobsters!
Thank you!
Nice. Similar pattern for heavily threaded code accessing shared state - split the variable into an array & ensure there’s a cacheline of space for each to avoid false sharing..
This is very similar to the pattern of putting counters in “thread-local storage” in a PC app; each thread increments its own private counter (which won’t be contended by anyone else), and it’s the reader’s responsibility to grab a coherent view of all of the counters and add them up. As long as you expect reads to be rare compared to increments, it works well.