Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Smart Pointers to boost your code

, 27 Sep 2004
A beginner's introduction to the smart pointers provided by the boost library.
#ifndef BOOST_DETAIL_LIGHTWEIGHT_MUTEX_HPP_INCLUDED
#define BOOST_DETAIL_LIGHTWEIGHT_MUTEX_HPP_INCLUDED

// MS compatible compilers support #pragma once

#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif

//
//  boost/detail/lightweight_mutex.hpp - lightweight mutex
//
//  Copyright (c) 2002, 2003 Peter Dimov and Multi Media Ltd.
//
//  Permission to copy, use, modify, sell and distribute this software
//  is granted provided this copyright notice appears in all copies.
//  This software is provided "as is" without express or implied
//  warranty, and with no claim as to its suitability for any purpose.
//
//  typedef <unspecified> boost::detail::lightweight_mutex;
//
//  boost::detail::lightweight_mutex meets a subset of the Mutex concept
//  requirements: http://www.boost.org/libs/thread/doc/mutex_concept.html#Mutex
//
//  * Used by the smart pointer library
//  * Performance oriented
//  * Header-only implementation
//  * Small memory footprint
//  * Not a general purpose mutex, use boost::mutex, CRITICAL_SECTION or
//    pthread_mutex instead.
//  * Never spin in a tight lock/do-something/unlock loop, since
//    lightweight_mutex does not guarantee fairness.
//  * Never keep a lightweight_mutex locked for long periods.
//
//  The current implementation can use a pthread_mutex, a CRITICAL_SECTION,
//  or a platform-specific spinlock.
//
//  You can force a particular implementation by defining BOOST_LWM_USE_PTHREADS,
//  BOOST_LWM_USE_CRITICAL_SECTION, or BOOST_LWM_USE_SPINLOCK.
//
//  If neither macro has been defined, the default is to use a spinlock on Win32,
//  and a pthread_mutex otherwise.
//
//  Note that a spinlock is not a general synchronization primitive. In particular,
//  it is not guaranteed to be a memory barrier, and it is possible to "livelock"
//  if a lower-priority thread has acquired the spinlock but a higher-priority
//  thread is spinning trying to acquire the same lock.
//
//  For these reasons, spinlocks have been disabled by default except on Windows,
//  where a spinlock can be several orders of magnitude faster than a CRITICAL_SECTION.


//  Note: lwm_linux.hpp has been disabled by default; see the comments
//        inside for more info.


#include <boost/config.hpp>

//  Note to implementors: if you write a platform-specific spinlock
//  for a platform that supports pthreads, be sure to test its performance
//  against the pthreads-based version using shared_ptr_timing_test.cpp and
//  shared_ptr_mt_test.cpp. Custom versions are usually not worth the trouble
//  _unless_ the performance gains are substantial.
//
//  Be sure to compare against a "real" pthreads library;
//  shared_ptr_timing_test.cpp will compile succesfully with a stub do-nothing
//  pthreads library, since it doesn't create any threads.

#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) && !defined(BOOST_LWM_USE_CRITICAL_SECTION) && !defined(BOOST_LWM_USE_PTHREADS)
# define BOOST_LWM_WIN32
#endif

#if !defined(BOOST_HAS_THREADS)
#  if defined(BOOST_LWM_WIN32)
#    include <boost/detail/lwm_win32_nt.hpp>
#  else
#    include <boost/detail/lwm_nop.hpp>
#  endif
#elif defined(BOOST_LWM_USE_SPINLOCK) && defined(BOOST_USE_ASM_ATOMIC_H)
#  include <boost/detail/lwm_linux.hpp>
#elif defined(BOOST_LWM_USE_CRITICAL_SECTION)
#  include <boost/detail/lwm_win32_cs.hpp>
#elif defined(BOOST_LWM_USE_PTHREADS)
#  include <boost/detail/lwm_pthreads.hpp>
#elif defined(BOOST_LWM_WIN32)
#  include <boost/detail/lwm_win32.hpp>
#elif defined(BOOST_LWM_USE_SPINLOCK) && defined(__sgi)
#  include <boost/detail/lwm_irix.hpp>
#elif defined(BOOST_LWM_USE_SPINLOCK) && defined(__GLIBCPP__)
#  include <boost/detail/lwm_gcc.hpp>
#elif defined(BOOST_HAS_PTHREADS)
#  define BOOST_LWM_USE_PTHREADS
#  include <boost/detail/lwm_pthreads.hpp>
#else
// Use #define BOOST_DISABLE_THREADS to avoid the error
#  error Unrecognized threading platform
#endif

#endif // #ifndef BOOST_DETAIL_LIGHTWEIGHT_MUTEX_HPP_INCLUDED

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

peterchen
Klippel
Germany Germany
Peter is tired of being called "Mr. Chen", even so certain individuals insist on it. No, he's not chinese.
 
Peter has seen lots of boxes you youngsters wouldn't even accept as calculators. He is proud of having visited the insides of a 16 Bit Machine.
 
In his spare time he ponders new ways of turning groceries into biohazards, or tries to coax South American officials to add some stamps to his passport.
 
Beyond these trivialities Peter works for Klippel[^], a small german company that wants to make mankind happier by selling them novel loudspeaker measurement equipment.
 

Where are you from?[^]



Please, if you are using one of my articles for anything, just leave me a comment. Seeing that this stuff is actually useful to someone is what keeps me posting and updating them.
Should you happen to not like it, tell me, too

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 27 Sep 2004
Article Copyright 2004 by peterchen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid