synchronized vs. C volatile

Lutger lutger.blijdestijn at gmail.com
Mon Aug 9 09:55:08 PDT 2010


SK wrote:

> On Mon, Aug 9, 2010 at 6:25 AM, dsimcha <dsimcha at yahoo.com> wrote:
>> == Quote from SK (sk at metrokings.com)'s article
>>> Does 'synchronized' mean the exact same thing as the C 'volatile'
>>> qualifier when applied to basic types?
>>> As in:
>>> synchronized int x;
>>
>> They're completely different.  synchronized is basically a scoped mutex and
>> applies to a statement, not a variable declaration.  I have no idea what your
>> example does, but it probably shouldn't even compile.  Volatile just prevents
>> certain compiler optimizations that can interfere with updates to a variable
>> from other hardware besides the CPU and is useful for things like device
>> drivers.
>>
>>
> 
> Sounds right to me too.  But the compiler warns about deprecated
> 'volatile'.  Is the warning a mistake?  Volatile has an important
> place in a systems language and I'd rather not mess with tradition
> here.
> 
> $ cat main.d
> int main()
> {
>         volatile int x = 1;
>         synchronized int y = 1;
>         return 0;
> }
> $ dmd -w -wi main.d
> main.d(4): volatile statements deprecated; used synchronized statements
> instead


I'm not an expert on this, but if I am not mistaken the shared type qualifier 
will handle the barriers, require atomic ops and prevent reordering compiler 
optimizations so it looks like this one will take care of what volatile used to 
do, right?


More information about the Digitalmars-d mailing list