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. >
