• Boost Thread学习笔记五


    多线程编程中还有一个重要的概念:Thread Local Store(TLS,线程局部存储),在boost中,TLS也被称作TSS,Thread Specific Storage。
    boost::thread库为我们提供了一个接口简单的TLS的面向对象的封装,以下是tss类的接口定义:

    class tss
    {
    public:
        tss(boost::function1<void, void*>* pcleanup);
        void* get() const;
        void set(void* value);
        void cleanup(void* p);
    };


    分别用于获取、设置、清除线程局部存储变量,这些函数在内部封装了TlsAlloc、TlsGetValue、TlsSetValue等API操作,将它们封装成了OO的形式。
    但boost将该类信息封装在detail名字空间内,即不推荐我们使用,当需要使用tss时,我们应该使用另一个使用更加方便的类:thread_specific_ptr,这是一个智能指针类,该类的接口如下:

     1 class thread_specific_ptr : private boost::noncopyable   // Exposition only
     2 {
     3 public:
     4   // construct/copy/destruct
     5   thread_specific_ptr();
     6   thread_specific_ptr(void (*cleanup)(void*));
     7   ~thread_specific_ptr();
     8 
     9   // modifier functions
    10   T* release();
    11   void reset(T* = 0);
    12 
    13   // observer functions
    14   T* get() const;
    15   T* operator->() const;
    16   T& operator*()() const;
    17 };


    即可支持get、reset、release等操作。
    thread_specific_ptr类的实现十分简单,仅仅为了将tss类“改装”成智 能指针的样子,该类在其构造函数中会自动创建一个tss对象,而在其析构函数中会调用默认参数的reset函数,从而引起内部被封装的tss对象被析构, 达到“自动”管理内存分配释放的目的。

    以下是一个运用thread_specific_ptr实现TSS的例子:

     1 #include <boost/thread/thread.hpp>
     2 #include <boost/thread/mutex.hpp>
     3 #include <boost/thread/tss.hpp>
     4 #include <iostream>
     5 
     6 boost::mutex io_mutex;
     7 boost::thread_specific_ptr<int> ptr;    // use this method to tell that this member will not shared by all threads
     8 
     9 struct count
    10 {
    11     count(int id) : id(id) { }
    12 
    13     void operator()()
    14     {
    15         if (ptr.get() == 0)    // if ptr is not initialized, initialize it
    16             ptr.reset(new int(0));    // Attention, we pass a pointer to reset (actually set ptr)
    17 
    18         for (int i = 0; i < 10; ++i)
    19         {
    20             (*ptr)++;
    21             boost::mutex::scoped_lock lock(io_mutex);
    22             std::cout << id << ": " << *ptr << std::endl;
    23         }
    24     }
    25 
    26     int id;
    27 };
    28 
    29 int main(int argc, char* argv[])
    30 {
    31     boost::thread thrd1(count(1));
    32     boost::thread thrd2(count(2));
    33     thrd1.join();
    34     thrd2.join();
    35 
    36     return 0;
    37 }

    此外,thread库还提供了一个很有趣的函数,call_once,在tss::init的实现中就用到了该函数。
    该函数的声明如下:
    void
     call_once(void (*func)(), once_flag& flag);
    该函数的Windows实现通过创建一个Mutex使所有的线程在尝试执行该函数时处于等待状态,直到有一个线程执行完了func函数,该函数的第二个参数表示函数func是否已被执行,该参数往往被初始化成BOOST_ONCE_INIT(即0),如果你将该参数初始化成1,则函数func将不被调用,此时call_once相当于什么也没干,这在有时候可能是需要的,比如,根据程序处理的结果决定是否需要call_once某函数func。
    call_once在执行完函数func后,会将flag修改为1,这样会导致以后执行call_once的线程(包括等待在Mutex处的线程和刚刚进入call_once的线程)都会跳过执行func的代码。

    需要注意的是,该函数不是一个模板函数,而是一个普通函数,它的第一个参数1是一个函数指针,其类型为void (*)(),而不是跟boost库的很多其它地方一样用的是function模板,不过这样也没有关系,有了boost::bind这个超级武器,想怎么绑定参数就随你的便了,根据boost的文档,要求传入的函数不能抛出异常,但从实现代码中好像不是这样。

    以下是一个典型的运用call_once实现一次初始化的例子:

     1 #include <boost/thread/thread.hpp>
     2 #include <boost/thread/once.hpp>
     3 #include <iostream>
     4 
     5 int i = 0;
     6 int j = 0;
     7 boost::once_flag flag = BOOST_ONCE_INIT;
     8 
     9 void init()
    10 {
    11     ++i;
    12 }
    13 
    14 void thread()
    15 {
    16     boost::call_once(&init, flag);
    17     ++j;
    18 }
    19 
    20 int main(int argc, char* argv[])
    21 {
    22     boost::thread thrd1(&thread);
    23     boost::thread thrd2(&thread);
    24     thrd1.join();
    25     thrd2.join();
    26 
    27     std::cout << i << std::endl;
    28     std::cout << j << std::endl;
    29 
    30     return 0;
    31 }

    结果显示,全局变量i仅被执行了一次++操作,而变量j则在两个线程中均执行了++操作。

  • 相关阅读:
    python下载文件(图片)源码,包含爬网内容(爬url),可保存cookie
    查看linux下各数据类型的大小
    linux 内核代码精简
    前序 中序 后序 遍历 递归 非递归算法 java实现
    netflix turbine概述
    How Hystrix Works?--官方
    netflix ribbon概述
    spring-cloud-netflix集成的服务
    支付系统设计包含:账户,对账,风控...!史上最全的!--转
    利用CORS实现跨域请求--转
  • 原文地址:https://www.cnblogs.com/lidabo/p/3796152.html
Copyright © 2020-2023  润新知