atomic_count_pthreads.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED
00003 
00004 //
00005 //  boost/detail/atomic_count_pthreads.hpp
00006 //
00007 //  Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
00008 //
00009 // Distributed under the Boost Software License, Version 1.0. (See
00010 // accompanying file LICENSE_1_0.txt or copy at
00011 // http://www.boost.org/LICENSE_1_0.txt)
00012 //
00013 
00014 #include <pthread.h>
00015 
00016 //
00017 //  The generic pthread_mutex-based implementation sometimes leads to
00018 //    inefficiencies. Example: a class with two atomic_count members
00019 //    can get away with a single mutex.
00020 //
00021 //  Users can detect this situation by checking BOOST_AC_USE_PTHREADS.
00022 //
00023 
00024 namespace boost
00025 {
00026 
00027 namespace detail
00028 {
00029 
00030 class atomic_count
00031 {
00032 private:
00033 
00034     class scoped_lock
00035     {
00036     public:
00037 
00038         scoped_lock(pthread_mutex_t & m): m_(m)
00039         {
00040             pthread_mutex_lock(&m_);
00041         }
00042 
00043         ~scoped_lock()
00044         {
00045             pthread_mutex_unlock(&m_);
00046         }
00047 
00048     private:
00049 
00050         pthread_mutex_t & m_;
00051     };
00052 
00053 public:
00054 
00055     explicit atomic_count(long v): value_(v)
00056     {
00057         pthread_mutex_init(&mutex_, 0);
00058     }
00059 
00060     ~atomic_count()
00061     {
00062         pthread_mutex_destroy(&mutex_);
00063     }
00064 
00065     long operator++()
00066     {
00067         scoped_lock lock(mutex_);
00068         return ++value_;
00069     }
00070 
00071     long operator--()
00072     {
00073         scoped_lock lock(mutex_);
00074         return --value_;
00075     }
00076 
00077     operator long() const
00078     {
00079         scoped_lock lock(mutex_);
00080         return value_;
00081     }
00082 
00083 private:
00084 
00085     atomic_count(atomic_count const &);
00086     atomic_count & operator=(atomic_count const &);
00087 
00088     mutable pthread_mutex_t mutex_;
00089     long value_;
00090 };
00091 
00092 } // namespace detail
00093 
00094 } // namespace boost
00095 
00096 #endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:28