Re: Named shared memory without synchronization





On 23 nov, 22:24, "Alex Blekhman" <x...@xxxxxxxxx> wrote:
"Dan Schwartz" wrote:

Is this really a stable solution?No. I'd suggest to use `volatile' specifier, so compiler
will be aware of possibility of asynchronous changes. In
your specific case optimizer didn't kick in for whatever
reason, but I wouldn't rely on that in the future.

I do not agree : The semantic of "volatile" as defined in the C
standard is so imprecise that it is almost useless. In any cases,
"volatile" is NOT a guarantee when delaling with multithreading
context. It MAY be usefull when writing low-level code that deals
directly with hardware (for example, directly reading a register that
is modified through some hardware piece). However, this is useless in a
Windows user application context, especially when multi-processors and
weak-cache semantic come into play.

As Igor has already explained, this doesn't hold true for VC8, which
gives precise and meaningfull semantic to volatile (ie : memory
barriers), but this is VC8-specific and is not portable.

To the OP : I would suggest accessing the shared variable through
Interlock* functions. (InterlockExchange for writing,
InterlockCompareExchange for reading). This avoid the cumbersom-ness of
adding a named mutex to protect the data, it is portable among
compilers and it is rather efficient at run-time. Of course, if you are
using VC8, making the variable volatile in both processes would be
equivalent.

Arnaud
MVP - VC

.



Relevant Pages

  • Re: When is "volatile" used instead of "lock" ?
    ... The volatile modifier is usually used for a field that is accessed by ... Not necessarily, there is no need for volatile, as long you Interlock consistently across all threads in the process. ... If the variable is not volatile, the compiler can actually call Interlocked on a local copy, and then write the value to the real variable once, at the end of the loop. ... volatile in lock free or low lock scenario's. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: When is "volatile" used instead of "lock" ?
    ... The volatile modifier is usually used for a field that is accessed by ... Not necessarily, there is no need for volatile, as long you Interlock consistently across all threads in the process. ... Without volatile semantics, the compiler is free to cache the value of any parameter, including in/out parameters. ... I also don't agree with your statement that you should *always* use volatile in lock free or low lock scenario's. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: When is "volatile" used instead of "lock" ?
    ... The volatile modifier is usually used for a field that is accessed by ... multiple threads without using the lock Statement ... Not necessarily, there is no need for volatile, as long you Interlock ... free to cache the value of any parameter, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: When is "volatile" used instead of "lock" ?
    ... The volatile modifier is usually used for a field that is accessed by ... statement to serialize access. ... Developers for exciting positions in medical product ... Not necessarily, there is no need for volatile, as long you Interlock consistently across all threads in the process. ...
    (microsoft.public.dotnet.languages.csharp)

Loading