Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #5144

Re: Please disprove this Double-Checked Locking "fix"

From Gerhard Fiedler <gelists@gmail.com>
Subject Re: Please disprove this Double-Checked Locking "fix"
Newsgroups comp.lang.c++
References <78f3178b-efdc-4af5-8f84-7ff6fa995af7@e25g2000prf.googlegroups.com> <d6774102-147b-4e3d-a457-a6bfd4c98a13@f15g2000pro.googlegroups.com> <5b81d426-bc25-46b2-b97d-0ea29355dcb1@z13g2000prk.googlegroups.com> <1bzq6ymjbt91c$.dlg@gelists.gmail.com> <4676ab04-fd5a-4c87-acea-63a951daf1b4@r35g2000prj.googlegroups.com>
Date 2011-05-18 18:12 -0300
Message-ID <qqe5nc4lm7rr.dlg@gelists.gmail.com> (permalink)
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


Joshua Maurice wrote:

> On May 13, 3:56 pm, Gerhard Fiedler <geli...@gmail.com> wrote:
>> Joshua Maurice wrote:
>>> However, a sufficiently smart compiler could notice your clever ruse,
>>> optimize away the assert as always true, see a lock and unlock pair
>>> guarding nothing, optimize that away, and then move the assignment to
>>> temp past the mutex acquire, as demonstrated above.
>>
>> Regarding the compiler optimizing away a lock/unlock pair guarding
>> "nothing": AIUI both lock and unlock need to provide certain fences.
>> Therefore, again AIUI, they can't be optimized away by the compiler even
>> if there's nothing in between, because that would remove the fences and
>> alter the behavior.
>>
>> Am I missing something here?
> 
> That may be how they're commonly implemented, but that's not the
> guaranteed semantics. Two different mutexes may as a matter of fact on
> a given implementation give "happens-before" effects between the two
> different mutexes, but there's nothing guaranteed about it.
> 
> ...
> 
> With that, it sees:
>   someMutex.lock();
>   <blah1>
>   someMutex.unlock();
>   <blah2>
>   someMutex.lock();
>   <blah3>
>   someMutex.unlock();
> 
> The compiler sees a lock, unlock, lock, unlock, in straightline code,
> without branching (or exceptions, or volatile (to keep signal handlers
> correct)). The compiler is totally free to replace that with:
>   someMutex.lock();
>   <blah1>
>   <blah2>
>   <blah3>
>   someMutex.unlock();

This is exactly what I don't understand -- I guess I have to look deeper
into the different mutex/lock implementations and their guarantees. I
was under the impression that through the lock/unlock pairs, it is
guaranteed that <blah1> happens before <blah2>, and that <blah3> happens
after it. 

> Now, back to my original much more controversial statement - can a
> compiler simply remove a lock unlock pair? Ex:
>   mutex.lock();
>   mutex.unlock();
> Maybe. I mentioned "clever ruse" with whole program optimization in
> mind. (However, upon thinking about it, I just showed that you don't
> even need whole program optimization.) Without whole program
> optimization, I think no. Could someone please more educated weigh
> in?
> 
> Thus far, after 10 minutes of attempts just now to write a conforming
> race-free program where you could tell the difference if a compiler
> simply removed an "empty" mutex acquire release pair, the only
> programs I can find are ones that would deadlock before the change,
> and not deadlock after the change. A deadlock is observable behavior,
> so a compiler cannot remove it for that reason.

Which would mean that if the compiler wants to remove it, it has to be
able to prove that it can't deadlock. This would be a useful diagnostic
output :)

Thanks,
Gerhard

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Please disprove this Double-Checked Locking "fix" jl_post@hotmail.com - 2011-04-26 09:58 -0700
  Re: Please disprove this Double-Checked Locking "fix" Leigh Johnston <leigh@i42.co.uk> - 2011-04-26 18:17 +0100
  Re: Please disprove this Double-Checked Locking "fix" Pete Becker <pete@versatilecoding.com> - 2011-04-26 13:50 -0400
    Re: Please disprove this Double-Checked Locking "fix" Scott Meyers <NeverRead@aristeia.com> - 2011-05-01 17:14 -0700
      Re: Please disprove this Double-Checked Locking "fix" Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-02 15:59 -0700
        Re: Please disprove this Double-Checked Locking "fix" Pete Becker <pete@versatilecoding.com> - 2011-05-03 08:39 -0400
  Re: Please disprove this Double-Checked Locking "fix" Joshua Maurice <joshuamaurice@gmail.com> - 2011-04-26 11:16 -0700
    Re: Please disprove this Double-Checked Locking "fix" Joshua Maurice <joshuamaurice@gmail.com> - 2011-04-26 11:19 -0700
    Re: Please disprove this Double-Checked Locking "fix" Pete Becker <pete@versatilecoding.com> - 2011-04-26 14:30 -0400
      Re: Please disprove this Double-Checked Locking "fix" Joshua Maurice <joshuamaurice@gmail.com> - 2011-04-26 11:50 -0700
    Re: Please disprove this Double-Checked Locking "fix" Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-11 19:55 -0700
      Re: Please disprove this Double-Checked Locking "fix" Gerhard Fiedler <gelists@gmail.com> - 2011-05-13 19:56 -0300
        Re: Please disprove this Double-Checked Locking "fix" Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-13 16:59 -0700
          Re: Please disprove this Double-Checked Locking "fix" Gerhard Fiedler <gelists@gmail.com> - 2011-05-18 18:12 -0300
            Re: Please disprove this Double-Checked Locking "fix" Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-18 14:53 -0700
              Re: Please disprove this Double-Checked Locking "fix" Gerhard Fiedler <gelists@gmail.com> - 2011-05-19 13:46 -0300
                Re: Please disprove this Double-Checked Locking "fix" Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-19 15:30 -0700
                Re: Please disprove this Double-Checked Locking "fix" Gerhard Fiedler <gelists@gmail.com> - 2011-05-21 11:55 -0300
                Re: Please disprove this Double-Checked Locking "fix" Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-22 01:08 -0700
  Re: Please disprove this Double-Checked Locking "fix" James Kanze <james.kanze@gmail.com> - 2011-04-30 15:54 -0700
    Re: Please disprove this Double-Checked Locking "fix" Leigh Johnston <leigh@i42.co.uk> - 2011-05-01 21:49 +0100
      Re: Please disprove this Double-Checked Locking "fix" Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2011-05-01 17:26 -0400
        Re: Please disprove this Double-Checked Locking "fix" Leigh Johnston <leigh@i42.co.uk> - 2011-05-01 22:44 +0100
          Re: Please disprove this Double-Checked Locking "fix" Leigh Johnston <leigh@i42.co.uk> - 2011-05-02 01:01 +0100
            Re: Please disprove this Double-Checked Locking "fix" Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2011-05-01 22:04 -0400
            Re: Please disprove this Double-Checked Locking "fix" "Chris M. Thomasson" <cristom@charter.net> - 2011-05-04 11:49 -0700
              Re: Please disprove this Double-Checked Locking "fix" Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2011-05-06 00:16 -0400
          Re: Please disprove this Double-Checked Locking "fix" Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-02 15:43 -0700
        Re: Please disprove this Double-Checked Locking "fix" James Kanze <james.kanze@gmail.com> - 2011-05-01 14:53 -0700
          Re: Please disprove this Double-Checked Locking "fix" Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2011-05-01 19:23 -0400
            Re: Please disprove this Double-Checked Locking "fix" James Kanze <james.kanze@gmail.com> - 2011-05-02 09:02 -0700
              Re: Please disprove this Double-Checked Locking "fix" Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2011-05-05 23:46 -0400
      Re: Please disprove this Double-Checked Locking "fix" James Kanze <james.kanze@gmail.com> - 2011-05-01 14:47 -0700

csiph-web