Re: Good practice for an approximation algorithm with start & stop features

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: TT (Tom Tempelaere) (_N_0SPA|/\|titi_____at_hotmail.com|/\|APS0_N_)
Date: 04/02/04


Date: Fri, 2 Apr 2004 06:01:09 -0800

Hi Jon,

I do not wish to reopen this discussion. But I was just browsing through IDesign's C# coding standard (v1.81) <http://www.idesign.net/idesign/download/IDesign%20CSharp%20Coding%20Standard.zip> and read the following guideline 4.4.19 (p20):

  "Do not use volatile variables, ..."

Notice that Juval doesn't write "Avoid ...". He should probably have said "Avoid".

Tom.

     ----- Jon Skeet [C# MVP] wrote: -----
     
     TT (Tom Tempelaere) <"=?Utf-8?B?VFQgKFRvbSBUZW1wZWxhZXJlKQ==?=" <_N_
     0SPA|/\|titi____@hotmail.com|/\|APS0_N_>> wrote:
> What you say is true, and I agree with you. Perhaps I'm still
> struggling too much with my c++ background. But I will have to take
> extra care not to mix the C# volatile with volatile in C/C++ when I
> have to do another project in C++. But that is part of the job I
> guess. I have to constantly be aware of these issues, because I'm
> dealing with different languages all the time. Another fact is that a
> lot of C# coders have a C++ background, and it is worth mentioning
> the difference of volatile in these languages because the languages look
> similar. That's why I mentioned it.
     
     Right. However, treating *anything* as the same between two languages
     is a potential problem. That's why I rigorously try to stick with the
     idioms, naming conventions etc of a new language - it helps me to think
     in that language, rather than one which happens to look like it :)
     
> I do (of course) hide any specifics of the event to the client, so
> that is not an issue. Asking the thread to finish is just calling a
> method, checking whether shutdown was requested is just another. Then
> one can implement it like he wants.
>> The event stopRunning or the bool stopRunning would have the same
> name. I agree that testing a boolean is more readable than waiting.
     
     Right.
      
> When you speak of locking, you mean that you would use a locking
> object to access the boolean instead of making it volatile? Like:
>> Object lockObject = new Object();
> bool stopRunning = false;
>> // signal:
> lock( lockObject ) { stopRunning= true; }
>> // check signal
> lock( lockObject ) { if(stopRunning) {...}}
>> Or did I get you wrong?
     
     Nope, that's exactly it. I'd put the actual lock within the property,
     but yes, that's the thing. The semantics of "lock" include a read
     memory barrier when acquiring, and a write memory barrier when
     releasing, so that you get volatile behaviour.
     
     --
     Jon Skeet - <skeet@pobox.com>
     http://www.pobox.com/~skeet
     If replying to the group, please do not mail me too
     



Relevant Pages

  • Re: Good practice for an approximation algorithm with start & stop features
    ... > dealing with different languages all the time. ... > the difference of volatile in these languages because the languages look ... I'd put the actual lock within the property, ... memory barrier when acquiring, and a write memory barrier when ...
    (microsoft.public.dotnet.languages.csharp)
  • Volatile, pthread mutexes, structure pointers, and non-locked reads
    ... It's unclear to me if I need to use volatile and/or add extra locking ... around a field of a struct to avoid the compiler optimizing away a read ... I've read that if I use locking around all accesses to a variable then ... pthread_mutex_unlock(globalMutex); ...
    (comp.programming.threads)
  • Re: x86 i/o management
    ... the hardware access and memory barrier ... The way I would implement volatile is by saying "volatile obviously was ... With all the language lawyering I came to the conclusion that you can't ...
    (comp.arch)
  • Re: "Crystallizing" Objects
    ... No need to think about locking there -- the language takes care of ... That's because there is no mutable data structure there, ... That is not possible in languages like ... Ruby where such things are abound and shared. ...
    (comp.lang.ruby)
  • Which functions act as memory barriers?
    ... the compiler can optimize and load shared ... Therefore, declaring shared_data as volatile ... And where can I read about this memory barrier concept? ...
    (comp.lang.c)