⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stl.h

📁 用c++包装好的线程库,直接拿来使用,提高效率.
💻 H
字号:
// Threading support -*- C++ -*-// This file is part of C++ threads, and provides threading// support for the standard template library.//#ifndef GCC_VERSION#define GCC_VERSION ( __GNUC__ * 1000 + __GNUC_MINOR__ )#endif#if (GCC_VERSION < 3000)# include <threads/alloc.h>#else/* * Copyright (c) 1997-1999 * Silicon Graphics Computer Systems, Inc. * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation.  Silicon Graphics makes no * representations about the suitability of this software for any * purpose.  It is provided "as is" without express or implied warranty. */#ifndef __SGI_STL_INTERNAL_THREADS_H#define __SGI_STL_INTERNAL_THREADS_H#define __STL_THREADS#include <threads/spinlock.h>extern "C" {# include <sys/types.h>};namespace std{  // Class _Refcount_Base provides a type, _RC_t, a data member,  // _M_ref_count, and member functions _M_incr and _M_decr, which perform  // atomic preincrement/predecrement.  The constructor initializes   // _M_ref_count.    struct _Refcount_Base  {    typedef size_t _RC_t;    volatile _RC_t _M_ref_count;    mutex_t _M_ref_count_lock;    _Refcount_Base(_RC_t __n) : _M_ref_count(__n) {};    void _M_incr()     {      mutex_lock(&_M_ref_count_lock);      ++_M_ref_count;      mutex_unlock(&_M_ref_count_lock);    }    _RC_t _M_decr()     {      mutex_lock(&_M_ref_count_lock);      volatile _RC_t __tmp = --_M_ref_count;      mutex_unlock(&_M_ref_count_lock);      return __tmp;    }  };  // We use a template here only to get a unique initialized instance.  template<int __dummy>  struct _Swap_lock_struct {    static mutex_t _S_swap_lock;  };  template<int __dummy>  mutex_t _Swap_lock_struct<__dummy>::_S_swap_lock = DEFAULTMUTEX;  // This should be portable, but performance is expected  // to be quite awful.  This really needs platform specific  // code.  inline unsigned long _Atomic_swap(unsigned long * __p, unsigned long __q)     {      mutex_lock(&_Swap_lock_struct<0>::_S_swap_lock);      unsigned long __result = *__p;      *__p = __q;      mutex_unlock(&_Swap_lock_struct<0>::_S_swap_lock);      return __result;    }  // Locking class.  Note that this class *does not have a constructor*.  // It must be initialized either statically, with __STL_MUTEX_INITIALIZER,  // or dynamically, by explicitly calling the _M_initialize member function.  // (This is similar to the ways that a pthreads mutex can be initialized.)  // There are explicit member functions for acquiring and releasing the lock.  // There is no constructor because static initialization is essential for  // some uses, and only a class aggregate (see section 8.5.1 of the C++  // standard) can be initialized that way.  That means we must have no  // constructors, no base classes, no virtual functions, and no private or  // protected members.  // Helper struct.  This is a workaround for various compilers that don't  // handle static variables in inline functions properly.  template <int __inst>    struct _STL_mutex_spin {      enum { __low_max = 30, __high_max = 1000 };      // Low if we suspect uniprocessor, high for multiprocessor.      static unsigned __max;      static unsigned __last;    };  template <int __inst>    unsigned _STL_mutex_spin<__inst>::__max = _STL_mutex_spin<__inst>::__low_max;  template <int __inst>    unsigned _STL_mutex_spin<__inst>::__last = 0;  struct _STL_mutex_lock  {    mutex_t _M_lock;    void _M_initialize()   { _M_lock = DEFAULTMUTEX; };    void _M_acquire_lock() { mutex_lock(&_M_lock);   };    void _M_release_lock() { mutex_unlock(&_M_lock); };  };# define __STL_MUTEX_INITIALIZER = { DEFAULTMUTEX }  // A locking class that uses _STL_mutex_lock.  The constructor takes a  // reference to an _STL_mutex_lock, and acquires a lock.  The  // destructor releases the lock.  It's not clear that this is exactly  // the right functionality.  It will probably change in the future.  struct _STL_auto_lock  {    _STL_mutex_lock& _M_lock;      _STL_auto_lock(_STL_mutex_lock& __lock) : _M_lock(__lock)    { _M_lock._M_acquire_lock(); }    ~_STL_auto_lock() { _M_lock._M_release_lock(); }    private:    void operator=(const _STL_auto_lock&);    _STL_auto_lock(const _STL_auto_lock&);  };} // namespace std#endif /* __SGI_STL_INTERNAL_THREADS_H */#endif // GCC_VERSION

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -