Thread: Double-Check Locking in C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    Quote Originally Posted by Codeplug View Post
    >> I am of course assuming that global_ptr has properly been declared volatile
    volatile has nothing to do with multi-threading programming - and DCL is still broken with it.

    http://www.aristeia.com/Papers/DDJ_J...04_revised.pdf

    gg
    Really nice paper. Probably one knows how this situation is improved under the new standard, as there is thread support now and they should have a new memory model. See: Foundations of the C++ Concurrency Memory Model, Hans-J. Boehm (2008)

    So the memory model changed, but is it already somewhere implemented? Is it improving anything due to Double-Checking?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Sanches View Post
    Really nice paper. Probably one knows how this situation is improved under the new standard, as there is thread support now and they should have a new memory model. See: Foundations of the C++ Concurrency Memory Model, Hans-J. Boehm (2008)

    So the memory model changed, but is it already somewhere implemented? Is it improving anything due to Double-Checking?
    Don't yet Codeplug make you paranoid. There is no big debate about volatile or memory reordering, at least among people with a clue. I think Codeplug is just trying to cover all the bases.

    There's no chance in hell the store to memory will happen after the mutex is unlocked, because the mutex surely uses release semantics when unlocking the synchronization object. If it didn't, it wouldn't be much of a mutex.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by brewbuck View Post
    There's no chance in hell the store to memory will happen after the mutex is unlocked, because the mutex surely uses release semantics when unlocking the synchronization object. If it didn't, it wouldn't be much of a mutex.
    A release doesn't do anything without an acquire to synchronize with, and the thing about DCL is that the second thread never acquires the mutex, and thus never synchronizes with the release.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    A release doesn't do anything without an acquire to synchronize with, and the thing about DCL is that the second thread never acquires the mutex, and thus never synchronizes with the release.
    Doesn't this fix it?

    Code:
    if(global_ptr == 0){
       mutex->Lock();
       if(global_ptr == 0)
       {
          tmp = new Object();
          writebarrier();
          global_ptr = tmp;
       }
       mutex->Unlock();
    }
    In other words, it wasn't the second thread's fault, it was the initializing thread's fault for not ensuring the ordering.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double check
    By calc in forum C Programming
    Replies: 2
    Last Post: 07-11-2009, 03:25 AM
  2. need someone to double check this for me (c)..
    By flamehead144 in forum C Programming
    Replies: 2
    Last Post: 02-16-2009, 12:19 PM
  3. Double-Checked Locking pattern issue
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2008, 04:29 AM
  4. can someone double check and maybe improve my code
    By tommy69 in forum C Programming
    Replies: 23
    Last Post: 04-21-2004, 02:04 PM
  5. Double check on Java code.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-05-2002, 09:55 AM

Tags for this Thread