C++Boost库学习之thread库(一)

目录

1.thread库概述
2.类thread
  ①类定义
  ②使用例子
3.命名空间this_thread
  ①空间内定义
  ②使用例子
4.类thread_group
  ①类定义
  ②使用例子

1.thread库 ^

  Boost.Thread允许在可移植C ++代码中使用多个执行线程和共享数据。它提供了用于管理线程本身的类和函数,以及用于在线程之间同步数据或提供特定于各个线程的数据的单独副本的其他类。

#include <boost/thread/thread.hpp>

namespace boost
{
  class thread;
  void swap(thread& lhs,thread& rhs) noexcept;

  namespace this_thread
  {
    thread::id get_id() noexcept;
    template<typename TimeDuration>
    void yield() noexcept;
    template <class Clock, class Duration>
    void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
    template <class Rep, class Period>
    void sleep_for(const chrono::duration<Rep, Period>& rel_time);
    namespace no_interruption_point  // 扩展
    {
        template <class Clock, class Duration>
        void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
        template <class Rep, class Period>
        void sleep_for(const chrono::duration<Rep, Period>& rel_time);
    }
    template<typename Callable>
    void at_thread_exit(Callable func); // 扩展

    void interruption_point(); // 扩展
    bool interruption_requested() noexcept; // 扩展
    bool interruption_enabled() noexcept; // 扩展
    class disable_interruption; // 扩展
    class restore_interruption; // 扩展

  #if defined BOOST_THREAD_USES_DATETIME
    template <TimeDuration>
    void sleep(TimeDuration const& rel_time);  // 扩展
    void sleep(system_time const& abs_time); // 扩展
  #endif
  }
  class thread_group; // 扩展

}

2.类thread ^

  ①类定义 ^

#include <boost/thread/thread.hpp>

class thread
{
public:
    class attributes; // EXTENSION

    thread() noexcept;
    ~thread();
    //删除拷贝构造函数
    thread(const thread&) = delete;
    thread& operator=(const thread&) = delete;

    //移动构造函数和移动赋值运算符
    thread(thread&&) noexcept;
    thread& operator=(thread&&) noexcept;

    //利用函数构造一个线程,也可以用lambda表达式
    template <class Callable>
    explicit thread(Callable func);
    template <class Callable>
    thread(Callable &&func);

    template <class F,class A1,class A2,...>
    thread(F f,A1 a1,A2 a2,...);
    template <class F, class ...Args>
    explicit thread(F&& f, Args&&... args);

    template <class Callable>
    explicit thread(attributes& attrs,Callable func); // EXTENSION

    template <class Callable>
    thread(attributes& attrs, Callable &&func); // EXTENSION

    template <class F, class ...Args>
    explicit thread(attributes& attrs, F&& f, Args&&... args);

    //交换线程
    void swap(thread& x) noexcept;

    class id;
    //获取线程id
    id get_id() const noexcept;
    //判断是否为可执行的线程
    bool joinable() const noexcept;
    //线程加入等待队列
    void join();
    //分离线程
    void detach();

    template <class Rep, class Period>
    bool try_join_for(const chrono::duration<Rep, Period>& rel_time); // EXTENSION
    template <class Clock, class Duration>
    bool try_join_until(const chrono::time_point<Clock, Duration>& t); // EXTENSION


    //当前系统可用的硬件线程数
    static unsigned hardware_concurrency() noexcept;
    //当前系统可用的物理内核数
    static unsigned physical_concurrency() noexcept;

    typedef platform-specific-type native_handle_type;
    //可以与特定于平台的API一起使用来操作底层实现
    native_handle_type native_handle();

    //设置一个中断点
    void interrupt(); // 扩展
    //如果调用了interrupt()则返回true,否则返回false
    bool interruption_requested() const noexcept; // 扩展


#if defined BOOST_THREAD_USES_DATETIME
    bool timed_join(const system_time& wait_until); // 弃用
    template<typename TimeDuration>
    bool timed_join(TimeDuration const& rel_time); // 弃用
    static void sleep(const system_time& xt);// 弃用
#endif

#if defined BOOST_THREAD_PROVIDES_THREAD_EQ

    bool operator==(const thread& other) const; // 弃用
    bool operator!=(const thread& other) const; // 弃用

#endif
    static void yield() noexcept; // 弃用

};

void swap(thread& lhs,thread& rhs) noexcept;

  ②使用例子 ^

#include<iostream>
#include <boost/thread/thread.hpp>
using namespace boost;
using std::cout;
using std::endl;
int main()
{
    thread t([] {for (int i = 0; i != 10; ++i) { cout << "子线程:" << i << endl; }});
    t.interrupt();
    if (t.interruption_requested())
    {
        cout << "中断" << endl;
    }
    if (t.joinable())
    {
        cout << "线程ID为:" << t.get_id() << endl;
        t.join();
    }
    cout << "当前系统可用的硬件线程数:" << t.hardware_concurrency() << endl;
    cout << "当前系统可用的物理内核数:" << t.physical_concurrency() << endl;    
}

3.命名空间this_thread ^

  ①空间内定义 ^

namespace boost {
  namespace this_thread {
    //返回当前正在执行的线程的ID
    thread::id get_id() noexcept;

    //放弃当前线程的时间片的剩余部分,以允许其他线程运行。
    template<typename TimeDuration>
    void yield() noexcept;

    //暂停当前线程直到指定的时间点abs_time
    template <class Clock, class Duration>
    void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
    //暂停线程rel_time时间
    template <class Rep, class Period>
    void sleep_for(const chrono::duration<Rep, Period>& rel_time);

    //将func放在线程的存储中,当前线程退出时调用此副本(即使线程已被中断)
    template<typename Callable>
    void at_thread_exit(Callable func);

    //请求中断线程
    void interruption_point(); 
    //如果当前线程请求了中断,则返回true,否则返回false
    bool interruption_requested() noexcept; 
    //如果已为当前线程启用了中断,则返回true,否则返回false
    bool interruption_enabled() noexcept; 

    //只有构造函数和祈构函数
    //构造函数:存储 当前线程的当前状态并禁用当前线程的中断
    //祈构函数:将当前线程的当前状态恢复为构造之前的状态。
    class disable_interruption; 
    //构造函数:将当前线程的当前状态恢复为构造之前的状态
    //祈构函数:禁用当前线程的中断
    class restore_interruption; 

  #if defined BOOST_THREAD_USES_DATETIME
    void sleep(TimeDuration const& rel_time); // 弃用
    void sleep(system_time const& abs_time);  // 弃用
  #endif
  }
}

  ②使用例子 ^

#include <boost/thread/thread.hpp>
using namespace boost::this_thread;
using std::cout;
using std::endl;
int main()
{
    at_thread_exit([] {for (int i = 0; i != 100; ++i)cout << "子线程:" << i << endl; });
    cout <<"线程ID:"<<get_id() << endl;
    cout << "主线程" << endl;  
}

4.类thread_group ^

  thread_group提供以某种方式相关的线程集合。可以使用add_thread 和create_thread成员函数将新线程添加到组中。thread_group不可复制或移动。

  ①类定义 ^

#include  < boost / thread / thread 。hpp > 
class  thread_group 
{ 
public :
    thread_group (const  thread_group &) =  delete ; 
    thread_group & operator =(const  thread_group &) =  delete ; 
    //创建一个没有线程的新线程组
    thread_group (); 
    ~ thread_group (); 

    //创建一个线程对象,并加入组中
    template < typename  F > 
    thread *  create_thread (F. threadfunc ); 
    //获取指向的对象的所有权,并将其添加到组中
    void  add_thread (thread *  thrd ); 
    //移除线程对象
    void  remove_thread (thread *  thrd ); 
    //如果组中有this_thread线程则返回true,否则返回false
    bool  is_this_thread_in (); 
    //判断一个线程是否在组中
    bool  is_thread_in (thread *  thrd ); 
    //调用组中的每个对象
    void  join_all (); 
    //请求中断组中的每个对象?
    void  interrupt_all (); 
    //组中的线程数
    int  size () const ; 
};

  ②使用例子 ^

#include<iostream>
#include <boost/thread/thread.hpp>
using namespace boost;
using std::cout;
using std::endl;
int main()
{
    thread *t=new thread([] {cout << "不在组中的线程" << endl; });
    thread_group tg;
    auto t1 = tg.create_thread([] {cout << "第一个线程" << endl; });
    auto t2 = tg.create_thread([] {cout << "第二个线程" << endl; });
    //tg.add_thread(t);
    tg.join_all();
    tg.interrupt_all();
    if (tg.is_thread_in(t1))
        cout << "t1在组里" << endl;
    if (tg.is_thread_in(t))
        cout << "t在组里" << endl;
    if (tg.is_this_thread_in())
        cout << "this_thread线程在组里" << endl;
    cout<<"组中的线程数:"<<tg.size()<<endl;
    tg.remove_thread(t1);   //将线程t1移出组中
}

猜你喜欢

转载自blog.csdn.net/qq_17044529/article/details/82599112