Ask a questionAsk a question
 

Answer"Locker" classes for concurrency runtime synchronisation primitives

  • Saturday, May 23, 2009 3:14 PMBarfy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It would be great to add a couple of locker classes to the library, the classes that are intended to wrap critical_section and reader_write_lock classes, so a "resource acquisition is object initialization" principle can be easily implemented.

    Something like this (or in fact a more generic solution would be appreciated):

    class critical_section_locker
    {
      critical_section &sec_;
    public:
      critical_section_locker(critical_section &sec) : sec_(sec)
      {
        sec_.lock();
      }

      ~critical_section_locker()
      {
        sec_.unlock();
      }
    };

    #define CRITICAL_SECTION_LOCK(x) critical_section_locker __csl_##LINE(x)

    A usage scenario:

    template<class T>
    class protected_set
    {
      std::set<T> data;
      Concurrency::critical_section sync;

    public:
      bool insert(T v)
      {
        CRITICAL_SECTION_LOCK(sync);
        auto result=data.insert(v);
        return result.second;  // lock is automatically released when execution goes out of scope
    // especially handy when there are severl return statements in a function, sophisticated error handling or exception handling
      }

Answers

  • Friday, May 29, 2009 3:57 AMrickmolloyMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Thanks for the suggestion, we're aware that using the critical_section and reader_writer without the RAII / scoped lock pattern is ill advised. 

    At some point we'll see std::lock_guard come in after C++0x is approved and of course critical_section & the write portion of reader_writer should *just work* also if you're using boost, boost::lock_guard is very similar to std::lock_guard and should just work directly.

    -Rick
    Rick Molloy Parallel Computing Platform : http://blogs.msdn.com/nativeconcurrency
    • Marked As Answer byBarfy Friday, May 29, 2009 6:32 AM
    •  

All Replies

  • Saturday, May 23, 2009 3:29 PMBarfy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer

    By "more generic" I mean something like this:

    template<class SyncT,void (SyncT::*LockMethod)(),void (SyncT::*UnlockMethod)()>
    class locker
    {
     SyncT &sync;

    public:
     locker(SyncT &sync_) throw() : sync(sync_)
     {
      (sync.*LockMethod)();
     }

     ~locker()
     {
      (sync.*UnlockMethod)();
     }
    };

    #define CS_LOCK(x) \
     locker<Concurrency::critical_section,&Concurrency::critical_section::lock,&Concurrency::critical_section::unlock> __csl_##LINE(x)

    #define RWL_LOCK(x) \
     locker<Concurrency::reader_writer_lock,&Concurrency::reader_writer_lock::lock,&Concurrency::reader_writer_lock::unlock> __rwll_##LINE(x)

    #define RWL_LOCK_READ(x) \
     locker<Concurrency::reader_writer_lock,&Concurrency::reader_writer_lock::lock_read,&Concurrency::reader_writer_lock::unlock> __rwll_##LINE(x)


    It would be great to see something like this natively in a library.

  • Friday, May 29, 2009 3:57 AMrickmolloyMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Thanks for the suggestion, we're aware that using the critical_section and reader_writer without the RAII / scoped lock pattern is ill advised. 

    At some point we'll see std::lock_guard come in after C++0x is approved and of course critical_section & the write portion of reader_writer should *just work* also if you're using boost, boost::lock_guard is very similar to std::lock_guard and should just work directly.

    -Rick
    Rick Molloy Parallel Computing Platform : http://blogs.msdn.com/nativeconcurrency
    • Marked As Answer byBarfy Friday, May 29, 2009 6:32 AM
    •  
  • Friday, May 29, 2009 6:30 AMBarfy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks, Rick!

    It would be nice to get more information on what other C++0x features are going to be implemented in next beta and final builds...

    BTW, I see you use an internal lock guard class in PPL headers :)
  • Friday, May 29, 2009 5:21 PMrickmolloyMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The vc team blog is a great resource for this http://blogs.msdn.com/vcblog, there is a series there which lists the language features and explains them. 

    In Beta1, the language features that are supported are auto, lambda expressions, decltype, r-value references and static_assert.  There's a smattering of C++0x library features as well, notably improvements in [algorithm] (all_of,none_of,etc), exception (exception_ptr) but I don't know the comprehensive list off the top of my head you might reask this question in the VC beta forum: http://social.msdn.microsoft.com/Forums/en-US/vcprerelease/threads.
    Rick Molloy Parallel Computing Platform : http://blogs.msdn.com/nativeconcurrency