home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,582 of 33,346   
   Alexander Terekhov to Dave Abrahams   
   Re: Double checked locking pattern artic   
   24 Oct 11 09:46:33   
   
   From: terekhov@web.de   
      
   Dave Abrahams wrote:   
   [...]   
   > Yes, basically.  There are some (expert-only) uses for "half-barriers"   
   > that allow movement of some operations, but mutex locks are basically   
   > always associated with strict, sequentially-consistent barriers.   
      
   I always thought that C++11 mutex locks can be expressed in terms of   
   relaxed C++11 atomic<> operations and acquire/release fences (not the   
   same as memory_order_seq_cst fence).   
      
   (pseudo-code, swap = exchange)   
      
   class swap_based_mutex_for_windows { // noncopyable   
      
     atomic m_lock_status; // 0: free, 1/-1: locked/contention   
     auto_reset_event m_retry_event; // slow binary semaphore (windows)   
      
   public:   
      
     // ctor/dtor [w/o lazy event init]   
      
     void lock() throw() {   
       if (m_lock_status.swap(1, memory_order_relaxed))   
         while (m_lock_status.swap(-1, memory_order_relaxed))   
           m_retry_event.wait();   
       atomic_thread_fence(memory_order_acquire);   
     }   
      
     void unlock() throw() {   
       atomic_thread_fence(memory_order_release);   
       if (m_lock_status.swap(0, memory_order_relaxed) < 0)   
         m_retry_event.set();   
     }   
      
   };   
      
   regards,   
   alexander.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca