::^乔乔^::明镜台::原创空间::C#.NET2.0,C++技术BLOG
人最重要的是心境,一颗平静安稳的心才能更好的进步,保持自己的心态.成为梦想中的高手QQ群:8664695
posts - 17,comments - 32,trackbacks - 0
Boost 类库是个开源的类库,传说中的准标准库这下将会是我阅读Boost 多线程源代码的心得.

费话少说,在使用BOOST的多线程我们都会引入boost/thread.hpp这一个文件好我们先找到这个文件将它打开

 1// Copyright (C) 2001-2003
 2// William E. Kempf
 3// (C) Copyright 2008 Anthony Williams
 4//
 5// Distributed under the Boost Software License, Version 1.0. (See accompanying 
 6// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 7
 8//  See www.boost.org/libs/thread for documentation.
 9
10#if !defined(BOOST_THREAD_WEK01082003_HPP)
11#define BOOST_THREAD_WEK01082003_HPP
12
13#include <boost/thread/thread.hpp>
14#include <boost/thread/condition_variable.hpp>
15#include <boost/thread/exceptions.hpp>
16#include <boost/thread/mutex.hpp>
17#include <boost/thread/once.hpp>
18#include <boost/thread/recursive_mutex.hpp>
19#include <boost/thread/tss.hpp>
20#include <boost/thread/thread_time.hpp>
21#include <boost/thread/locks.hpp>
22#include <boost/thread/shared_mutex.hpp>
23#include <boost/thread/barrier.hpp>
24
25#endif

从这些代码可以看出,boost/thread.hpp只是引用文件而已,看不到任何世界的C++代码,
我们顺着这些#include一个一个打开
先是#include <boost/thread/thread.hpp>

 1#ifndef BOOST_THREAD_THREAD_HPP
 2#define BOOST_THREAD_THREAD_HPP
 3
 4//  thread.hpp
 5//
 6//  (C) Copyright 2007-8 Anthony Williams 
 7//
 8//  Distributed under the Boost Software License, Version 1.0. (See
 9//  accompanying file LICENSE_1_0.txt or copy at
10//  http://www.boost.org/LICENSE_1_0.txt)
11
12#include <boost/thread/detail/platform.hpp>
13
14#if defined(BOOST_THREAD_PLATFORM_WIN32)
15#include <boost/thread/win32/thread_data.hpp>
16#elif defined(BOOST_THREAD_PLATFORM_PTHREAD)
17#include <boost/thread/pthread/thread_data.hpp>
18#else
19#error "Boost threads unavailable on this platform"
20#endif
21
22#include <boost/thread/detail/thread.hpp>
23
24
25#endif
26

明显这里是一个对不同平台作出的定义,

boost/thread/detail/是一个基本由宏定义组成的HPP文件,我们站且跳过
我们以WIN32入手...
#include <boost/thread/win32/thread_data.hpp>
  1#ifndef BOOST_THREAD_PTHREAD_THREAD_DATA_HPP
  2#define BOOST_THREAD_PTHREAD_THREAD_DATA_HPP
  3// Distributed under the Boost Software License, Version 1.0. (See
  4// accompanying file LICENSE_1_0.txt or copy at
  5// http://www.boost.org/LICENSE_1_0.txt)
  6// (C) Copyright 2007 Anthony Williams
  7
  8#include <boost/thread/detail/config.hpp>
  9#include <boost/thread/exceptions.hpp>
 10#include <boost/shared_ptr.hpp>
 11#include <boost/enable_shared_from_this.hpp>
 12#include <boost/thread/mutex.hpp>
 13#include <boost/optional.hpp>
 14#include <pthread.h>
 15#include "condition_variable_fwd.hpp"
 16
 17#include <boost/config/abi_prefix.hpp>
 18
 19namespace boost
 20{
 21    class thread;
 22    
 23    namespace detail
 24    {
 25        struct thread_exit_callback_node;
 26        struct tss_data_node;
 27
 28        struct thread_data_base;
 29        typedef boost::shared_ptr<thread_data_base> thread_data_ptr;
 30        
 31        struct BOOST_THREAD_DECL thread_data_base:
 32            enable_shared_from_this<thread_data_base>
 33        {
 34            thread_data_ptr self;
 35            pthread_t thread_handle;
 36            boost::mutex data_mutex;
 37            boost::condition_variable done_condition;
 38            boost::mutex sleep_mutex;
 39            boost::condition_variable sleep_condition;
 40            bool done;
 41            bool join_started;
 42            bool joined;
 43            boost::detail::thread_exit_callback_node* thread_exit_callbacks;
 44            boost::detail::tss_data_node* tss_data;
 45            bool interrupt_enabled;
 46            bool interrupt_requested;
 47            pthread_cond_t* current_cond;
 48
 49            thread_data_base():
 50                done(false),join_started(false),joined(false),
 51                thread_exit_callbacks(0),tss_data(0),
 52                interrupt_enabled(true),
 53                interrupt_requested(false),
 54                current_cond(0)
 55            {}
 56            virtual ~thread_data_base();
 57
 58            typedef pthread_t native_handle_type;
 59
 60            virtual void run()=0;
 61        }
;
 62
 63        BOOST_THREAD_DECL thread_data_base* get_current_thread_data();
 64
 65        class interruption_checker
 66        {
 67            thread_data_base* const thread_info;
 68
 69            void check_for_interruption()
 70            {
 71                if(thread_info->interrupt_requested)
 72                {
 73                    thread_info->interrupt_requested=false;
 74                    throw thread_interrupted();
 75                }

 76            }

 77            
 78            void operator=(interruption_checker&);
 79        public:
 80            explicit interruption_checker(pthread_cond_t* cond):
 81                thread_info(detail::get_current_thread_data())
 82            {
 83                if(thread_info && thread_info->interrupt_enabled)
 84                {
 85                    lock_guard<mutex> guard(thread_info->data_mutex);
 86                    check_for_interruption();
 87                    thread_info->current_cond=cond;
 88                }

 89            }

 90            ~interruption_checker()
 91            {
 92                if(thread_info && thread_info->interrupt_enabled)
 93                {
 94                    lock_guard<mutex> guard(thread_info->data_mutex);
 95                    thread_info->current_cond=NULL;
 96                    check_for_interruption();
 97                }

 98            }

 99        }
;
100    }

101
102    namespace this_thread
103    {
104        void BOOST_THREAD_DECL yield();
105        
106        void BOOST_THREAD_DECL sleep(system_time const& abs_time);
107        
108        template<typename TimeDuration>
109        inline void sleep(TimeDuration const& rel_time)
110        {
111            this_thread::sleep(get_system_time()+rel_time);
112        }

113    }

114}

115
116#include <boost/config/abi_suffix.hpp>
117
118#endif

水平有限....貌似停留在只能自己看懂....
posted on 2009-05-09 20:43 ^乔乔^ 阅读(1916) 评论(1)  编辑 收藏 引用 所属分类: c++

FeedBack:
# re: Boost 多线程源代码解读
2015-06-15 23:20 | crax
我对此表示很晕  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理