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

📄 threadcontext.hpp

📁 "More for C++" is a class library that provides some features that are usually common for object ori
💻 HPP
字号:
////  This file is part of the "More for C++" library////  Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@morefor.org)////  The "More for C++" library is free software; you can redistribute it and/or//  modify it under the terms of the license that comes with this package.////  Read "license.txt" for more details.////  THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED//  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES//  OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#ifndef MORE_UTIL_THREADCONTEXT_HPP#define MORE_UTIL_THREADCONTEXT_HPP////////////////////////////////////////////////////////////////////////////////#include <more/create.hpp>#include <more/exception.hpp>#include <more/stl.hpp>#include <more/synchronized.hpp>#include <more/os/mutex.hpp>#include <more/os/thread.hpp>#include <more/util/singleton.hpp>////////////////////////////////////////////////////////////////////////////////namespace more{  namespace util  {    class UnknownThreadContext: public more::Exception    {      public:        virtual operator const char* ( ) const;    };    template<typename Data>    class ThreadContextBinder    {      public:        ThreadContextBinder( const Data& );       ~ThreadContextBinder( );    };    template<typename Data>    class ThreadContextHolder: public Singleton<ThreadContextHolder<Data> >    {      public:        static Data getBoundData( );        ThreadContextHolder( );      private:        friend class ThreadContextBinder<Data>;        typedef size_t ThreadId;        typedef more::stl::Map<ThreadId, Data> ContextMap;        typedef typename ContextMap::const_iterator ContextMapIterator;        p<more::os::Mutex>  m_pMutex;        p<ContextMap>       m_pContextMap;    };  }}////////////////////////////////////////////////////////////////////////////////template<typename Data>inline more::util::ThreadContextBinder<Data>::ThreadContextBinder(  const Data& rData){  ThreadContextHolder<Data>* pInstance;  pInstance = Singleton<ThreadContextHolder<Data> >::getInstance( );  synchronized( pInstance -> m_pMutex )  {    ( *pInstance -> m_pContextMap )[more::os::Thread::getId( )] = rData;  } end_synchronized}////////////////////////////////////////////////////////////////////////////////template<typename Data>inline more::util::ThreadContextBinder<Data>::~ThreadContextBinder( ){  ThreadContextHolder<Data>* pInstance;  pInstance = Singleton<ThreadContextHolder<Data> >::getInstance( );  synchronized( pInstance -> m_pMutex )  {    pInstance -> m_pContextMap -> erase( more::os::Thread::getId( ) );  } end_synchronized}////////////////////////////////////////////////////////////////////////////////template<typename Data>inline more::util::ThreadContextHolder<Data>::ThreadContextHolder( ): m_pMutex( more::os::Mutex::create( ) ), m_pContextMap( CREATE ContextMap( ) ){  Singleton<ThreadContextHolder<Data> >::setInstance( this );}////////////////////////////////////////////////////////////////////////////////template<typename Data>inline Data more::util::ThreadContextHolder<Data>::getBoundData( ){  Data                        result;  ThreadContextHolder<Data>*  pInstance;  pInstance = Singleton<ThreadContextHolder<Data> >::getInstance( );  synchronized( pInstance -> m_pMutex )  {    ContextMapIterator pContextMapIterator;    pContextMapIterator = pInstance -> m_pContextMap -> find( more::os::Thread::getId( ) );    if( pContextMapIterator == pInstance -> m_pContextMap -> end( ) )    {      throw UnknownThreadContext( );    }    result = pContextMapIterator -> second;  } end_synchronized  return result;}////////////////////////////////////////////////////////////////////////////////#endif

⌨️ 快捷键说明

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