首页 > 代码库 > Boost Thread学习笔记四
Boost Thread学习笔记四
barrier
barrier类的接口定义如下:
1 class barrier : private boost::noncopyable // Exposition only
2 {
3 public:
4 // construct/copy/destruct
5 barrier(size_t n);
6 ~barrier();
7
8 // waiting
9 bool wait();
10 };
2 {
3 public:
4 // construct/copy/destruct
5 barrier(size_t n);
6 ~barrier();
7
8 // waiting
9 bool wait();
10 };
barrier类为我们提供了这样一种控制线程同步的机制:
前n - 1次调用wait函数将被阻塞,直到第n次调用wait函数,而此后第n + 1次到第2n - 1次调用wait也会被阻塞,直到第2n次调用,依次类推。
barrier::wait的实现十分简单:
1 barrier::barrier(unsigned int count)
2 : m_threshold(count), m_count(count), m_generation(0)
3 {
4 if (count == 0)
5 throw std::invalid_argument("count cannot be zero.");
6 }
7
8 bool barrier::wait()
9 {
10 boost::mutex::scoped_lock lock(m_mutex); // m_mutex is the base of barrier and is initilized by it‘s default constructor.
11 unsigned int gen = m_generation; // m_generation will be 0 for call 1~n-1, and 1 for n~2n - 1, and so on
12
13 if (--m_count == 0)
14 {
15 m_generation++; // cause m_generation to be changed in call n/2n/
16 m_count = m_threshold; // reset count
17 m_cond.notify_all(); // wake up all thread waiting here
18 return true;
19 }
20
21 while (gen == m_generation) // if m_generation is not changed, lock current thread.
22 m_cond.wait(lock);
23 return false;
24 }
2 : m_threshold(count), m_count(count), m_generation(0)
3 {
4 if (count == 0)
5 throw std::invalid_argument("count cannot be zero.");
6 }
7
8 bool barrier::wait()
9 {
10 boost::mutex::scoped_lock lock(m_mutex); // m_mutex is the base of barrier and is initilized by it‘s default constructor.
11 unsigned int gen = m_generation; // m_generation will be 0 for call 1~n-1, and 1 for n~2n - 1, and so on
12
13 if (--m_count == 0)
14 {
15 m_generation++; // cause m_generation to be changed in call n/2n/
16 m_count = m_threshold; // reset count
17 m_cond.notify_all(); // wake up all thread waiting here
18 return true;
19 }
20
21 while (gen == m_generation) // if m_generation is not changed, lock current thread.
22 m_cond.wait(lock);
23 return false;
24 }
因此,说白了也不过是mutex的一个简单应用。
以下是一个使用barrier的例子:
1 #include <boost/thread/thread.hpp>
2 #include <boost/thread/barrier.hpp>
3
4 int i = 0;
5 boost::barrier barr(3); // call barr.wait 3 * n times will release all threads in waiting
6
7 void thread()
8 {
9 ++i;
10 barr.wait();
11 }
12
13 int main()
14 {
15 boost::thread thrd1(&thread);
16 boost::thread thrd2(&thread);
17 boost::thread thrd3(&thread);
18
19 thrd1.join();
20 thrd2.join();
21 thrd3.join();
22
23 return 0;
24 }
2 #include <boost/thread/barrier.hpp>
3
4 int i = 0;
5 boost::barrier barr(3); // call barr.wait 3 * n times will release all threads in waiting
6
7 void thread()
8 {
9 ++i;
10 barr.wait();
11 }
12
13 int main()
14 {
15 boost::thread thrd1(&thread);
16 boost::thread thrd2(&thread);
17 boost::thread thrd3(&thread);
18
19 thrd1.join();
20 thrd2.join();
21 thrd3.join();
22
23 return 0;
24 }
如果去掉其中thrd3相关的代码,将使得线程1、2一直处于wait状态,进而使得主线程无法退出。
xtime
xtime是boost::thread中用来表示时间的一个辅助类,它是一个仅包含两个成员变量的结构体:
1 struct xtime
2 {
3 //
4 xtime_sec_t sec;
5 xtime_nsec_t nsec;
6 };
2 {
3 //
4 xtime_sec_t sec;
5 xtime_nsec_t nsec;
6 };
condition::timed_wait、thread::sleep等涉及超时的函数需要用到xtime。
需要注意的是,xtime表示的不是一个时间间隔,而是一个时间点,因此使用起来很不方便。为了方便使用xtime,boost提供了一些辅助的xtime操作函数,如xtime_get、xtime_cmp等。
以下是一个使用xtime来执行sleep的例子(跟简单的一句Sleep比起来,实在是太复杂了),其中用到了xtime初始化函数xtime_get:
1 #include <boost/thread/thread.hpp>
2 #include <boost/thread/xtime.hpp>
3 #include <iostream>
4
5 int main()
6 {
7 boost::xtime xt;
8 boost::xtime_get(&xt, boost::TIME_UTC); // initialize xt with current time
9 xt.sec += 1; // change xt to next second
10 boost::thread::sleep(xt); // do sleep
11
12 std::cout << "1 second sleep over." << std::endl;
13
14 return 0;
15 }
2 #include <boost/thread/xtime.hpp>
3 #include <iostream>
4
5 int main()
6 {
7 boost::xtime xt;
8 boost::xtime_get(&xt, boost::TIME_UTC); // initialize xt with current time
9 xt.sec += 1; // change xt to next second
10 boost::thread::sleep(xt); // do sleep
11
12 std::cout << "1 second sleep over." << std::endl;
13
14 return 0;
15 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。