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![](/Images/OutliningIndicators/None.gif)
8
// See www.boost.org/libs/thread for documentation.
9![](/Images/OutliningIndicators/None.gif)
10
#if !defined(BOOST_THREAD_WEK01082003_HPP)
11
#define BOOST_THREAD_WEK01082003_HPP
12![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/None.gif)
12
#include <boost/thread/detail/platform.hpp>
13![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/None.gif)
22
#include <boost/thread/detail/thread.hpp>
23![](/Images/OutliningIndicators/None.gif)
24![](/Images/OutliningIndicators/None.gif)
25
#endif
26![](/Images/OutliningIndicators/None.gif)
明显这里是一个对不同平台作出的定义,
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![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/None.gif)
17
#include <boost/config/abi_prefix.hpp>
18![](/Images/OutliningIndicators/None.gif)
19
namespace boost
20![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
21
class thread;
22
23
namespace detail
24![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
struct thread_exit_callback_node;
26
struct tss_data_node;
27![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{}
56
virtual ~thread_data_base();
57![](/Images/OutliningIndicators/InBlock.gif)
58
typedef pthread_t native_handle_type;
59![](/Images/OutliningIndicators/InBlock.gif)
60
virtual void run()=0;
61
};
62![](/Images/OutliningIndicators/InBlock.gif)
63
BOOST_THREAD_DECL thread_data_base* get_current_thread_data();
64![](/Images/OutliningIndicators/InBlock.gif)
65
class interruption_checker
66![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
67
thread_data_base* const thread_info;
68![](/Images/OutliningIndicators/InBlock.gif)
69
void check_for_interruption()
70![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
71
if(thread_info->interrupt_requested)
72![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83
if(thread_info && thread_info->interrupt_enabled)
84![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
if(thread_info && thread_info->interrupt_enabled)
93![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
94
lock_guard<mutex> guard(thread_info->data_mutex);
95
thread_info->current_cond=NULL;
96
check_for_interruption();
97
}
98
}
99
};
100
}
101![](/Images/OutliningIndicators/InBlock.gif)
102
namespace this_thread
103![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
111
this_thread::sleep(get_system_time()+rel_time);
112
}
113
}
114
}
115![](/Images/OutliningIndicators/None.gif)
116
#include <boost/config/abi_suffix.hpp>
117![](/Images/OutliningIndicators/None.gif)
118
#endif
水平有限....貌似停留在只能自己看懂....
posted on 2009-05-09 20:43
^乔乔^ 阅读(1935)
评论(1) 编辑 收藏 引用 所属分类:
c++