common types + type modifiers

Michel Fortin michel.fortin at michelf.com
Sun Jan 30 13:55:56 PST 2011


On 2011-01-30 13:23:16 -0500, Ellery Newcomer 
<ellery-newcomer at utulsa.edu> said:

> Ah, thank you. That clears things up a bit. how do you safely convert 
> from a shared type to a non shared type or vice versa? a deep copy 
> inside a critical section?

A copy? Yes. A critical section? Not really.

Shared is about atomic operations, things that the processor can do 
atomically without locking. They're used mostly to provide sequential 
consistency in our multi-core world where multiple cores can see a 
different value for the same memory address at one given time due to 
out-of-sync caches. Atomic ops forces the caches to synchronize but are 
slower.

You can't mix atomic operations with OS-level locking because they 
don't respect each other (a lock won't prevent an atomic read from 
occurring). Also, atomic ops are generally limited to word-sized data 
structures, sometime double-word. So on a 32-bit processor that's a 
32-bit or 64-bit value (assuming the value is properly aligned).

If you want to use locking (a critical section in Windows parlance), 
you should put your data in a synchronized class, then share that class 
with other threads. If you don't use locking, you have at best 
double-word granularity.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d-learn mailing list