In order to be compatible with different platforms, this class uses the Impl mode, win platform through the critical_section, such words or relatively simple, specific to the unknown solution, but have to say that the way to achieve the boost will be complex to where to go, of course, the benefits are powerful, The disadvantage is the high cost of learning, will use less people.
#if defined (os_win) #include <windows.h> #elif defined (os_posix) #include <pthread.h> #endif # include "base/ Base_export.h "#include" base/basictypes.h "namespace base {namespace Internal {//This class implements the underlying PLA Tform-specific Spin-lock mechanism//used for the lock class. Most users should not use Lockimpl directly, but//should instead use Lock.class base_export lockimpl {public: #if defined (os_win) typedef critical_section NATIVEHANDLE, #elif defined (os_posix) typedef pthread_mutex_t Nativehandle; #endif Loc Kimpl (); ~lockimpl (); If The lock is not a held, take it and return true. If the lock is already//held by something else, immediately return false. BOOL Try (); Take the lock and blocking until it is available if necessary. void Lock (); Release the lock. This must is called by the Lock's holder:after//a successful call to Try, or a call to lock. void Unlock (); Return the native underlying lock. TODO (awalker): Refactor lock and condition variables So, this is//unnecessary. nativehandle* Native_handle () {return &native_handle_;} private:nativehandle native_handle_; Disallow_copy_and_assign (Lockimpl);};} namespace internal}//namespace BASE#ENDIF//Base_synchronization_lock_impl_h_
Google Base's Lockimpl