One more thing I'd like to point out here that MPark neglected because he's
too humble: *the latest implementation of the synchronized macro enables us
to use a return statement within the block without having to add the
'UNREACHABLE();' statement after the block!*

I haven't seen it done in other projects that have a similar macros, very
cool stuff MPark! Thank you!


On Thu, Jul 9, 2015 at 5:02 PM Benjamin Mahler <[email protected]>
wrote:

> Thanks Michael, please do keep sending things like this out, and encourage
> others to do so as well.
>
> Going to be increasingly important to let each other know about things like
> this as the community grows. :)
>
> On Thu, Jul 9, 2015 at 4:37 PM, Marco Massenzio <[email protected]>
> wrote:
>
> > sweet!
> >
> > *Marco Massenzio*
> > *Distributed Systems Engineer*
> >
> > On Wed, Jul 8, 2015 at 12:40 PM, Michael Park <[email protected]> wrote:
> >
> > > Hello,
> > >
> > > This is an announcement about our *synchronized* macro available in
> > > *<stout/synchronized.hpp>*. *synchronized* is essentially a syntactic
> > sugar
> > > to the *std::lock_guard* pattern that is reminiscent of Java's
> > > *synchronized*.
> > >
> > > With *std::lock_guard*:
> > >
> > > *  std::mutex m;*
> > > *  {*
> > > *    std::lock_guard<std::mutex> lock(m);  **// Acquire.*
> > > *    // Do something under the lock.*
> > > *  }  **// Release.*
> > >
> > > With *synchronized*:
> > >
> > > *  std::mutex m;*
> > > *  synchronized (m) {**  // Acquire.*
> > > *    // Do something under the lock.*
> > > *  }**  // Release.*
> > >
> > > *synchronized* is currently supported for
> > >
> > >    - Any *T* that has *T::lock()* and *T::unlock()* (practically these
> > >    include *std::mutex, std::recursive_mutex*, etc)
> > >    - *std::atomic_flag* (remember to initialize it to
> *ATOMIC_FLAG_INIT*)
> > >    - *pthread_mutex_t* (we'll be deprecating this as soon as possible)
> > >
> > > If you find that you need additional support for your lock type *T*,
> you
> > > can add it easily by adding a *synchronize* function. It should follow
> > this
> > > pattern:
> > >
> > > *  Synchronized<T> synchronize(T* t)*
> > > *  {*
> > > *    return Synchronized<T>(*
> > > *        t,*
> > > *        [](T *t) { /* lock 't'. */ },*
> > > *        [](T *t) { /* unlock 't'. */ });*
> > > *  }*
> > >
> > > Great example of this is available in
> > > *3rdparty/libprocess/src/libevent_ssl_socket.hpp
> > > *for customized locking of *libevent*'s *bufferevent*.
> > >
> > > *  Synchronized<bufferevent> synchronize(bufferevent* bev)*
> > > *  {*
> > > *    return Synchronized<bufferevent>(*
> > > *        bev,*
> > >  *       [](bufferevent* bev) { bufferevent_lock(bev); },*
> > > *        [](bufferevent* bev) { bufferevent_unlock(bev); });*
> > > *  }*
> > >
> > > We use this mechanism consistently throughout the codebase, and would
> > like
> > > it to be the standard pattern for locking critical sections.
> > >
> > > Thanks,
> > >
> > > MPark.
> > >
> >
>

Reply via email to