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

目录

1.Scoped Threads
2.结构体与类strict_scoped_thread
  ①定义
  ②使用例子
3.类scoped_thread
  ①类定义
  ②使用例子

1.Scoped Threads ^

  Scoped Threads是一个线程的包装器,允许用户说明在销毁时要做什么。其中一个常见用途是在销毁时加入线程,因此这是默认行为。

//#include <boost/thread/scoped_thread.hpp>

struct detach;
struct join_if_joinable;
struct interrupt_and_join_if_joinable;
template <class CallableThread = join_if_joinable, class Thread = thread>
class strict_scoped_thread;
template <class CallableThread = join_if_joinable, class Thread = thread>
class scoped_thread;
template <class CallableThread, class Thread = thread>
void swap(scoped_thread<Callable, Thread>& lhs, scoped_threadCallable, Thread>& rhs) noexcept;

2.结构体与类strict_scoped_thread ^

  ①定义 ^

struct  detach 
{ 
  template  < class  Thread > 
  void  operator ()(Thread & t )
  { 
    t 。detach (); 
  } 
};

struct join_if_joinable
{
  template <class Thread>
  void operator()(Thread& t)
  {
    if (t.joinable())
    {
      t.join();
    }
  }
};

struct interrupt_and_join_if_joinable
{
  template <class Thread>
  void operator()(Thread& t)
  {
    t.interrupt();
    if (t.joinable())
    {
      t.join();
    }
  }
};

template <class CallableThread = join_if_joinable, class Thread = ::boost::thread>
class strict_scoped_thread
{
  thread t_; 
public:

  strict_scoped_thread(strict_scoped_thread const&) = delete;
  strict_scoped_thread& operator=(strict_scoped_thread const&) = delete;

  explicit strict_scoped_thread(Thread&& t) noexcept;
  template <typename F&&, typename ...Args>
  explicit strict_scoped_thread(F&&, Args&&...);

  ~strict_scoped_thread();

};

  ②使用例子 ^

#include <boost/thread/scoped_thread.hpp>
using namespace boost;
using std::cout;
using std::endl;
int main()
{
    //模板参数可以是上述三个结构体中任意一个
    strict_scoped_thread<> t(thread([] {cout << "子线程" << endl; }));
    cout << "主线程" << endl;
    for (int i = 0; i != 100; ++i)
        cout << i << endl;
}

3.类scoped_thread ^

  scoped_thread提供thread与所有操作相同的接口并转发所有操作。不过scoped_thread是一个类模板,可以用到上面的三个结构体

  ①类定义 ^

#include <boost/thread/scoped_thread.hpp>

template <class CallableThread, class Thread = thread>
class scoped_thread
{
  thread t_; // for exposition purposes only
public:
    scoped_thread() noexcept;
    scoped_thread(const scoped_thread&) = delete;
    scoped_thread& operator=(const scoped_thread&) = delete;

    explicit scoped_thread(thread&& th) noexcept;
    template <typename F&&, typename ...Args>
    explicit scoped_thread(F&&, Args&&...);

    ~scoped_thread();

    //移动构造函数及移动赋值运算符
    scoped_thread(scoped_thread && x) noexcept;
    scoped_thread& operator=(scoped_thread && x) noexcept;

    void swap(scoped_thread& x) noexcept;

    typedef thread::id id;

    id get_id() const noexcept;

    bool joinable() const noexcept;
    void join();
#ifdef BOOST_THREAD_USES_CHRONO
    template <class Rep, class Period>
    bool try_join_for(const chrono::duration<Rep, Period>& rel_time);
    template <class Clock, class Duration>
    bool try_join_until(const chrono::time_point<Clock, Duration>& t);
#endif

    void detach();

    static unsigned hardware_concurrency() noexcept;
    static unsigned physical_concurrency() noexcept;

    typedef thread::native_handle_type native_handle_type;
    native_handle_type native_handle();

#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
    void interrupt();
    bool interruption_requested() const noexcept;
#endif


};

template <class CallableThread, class Thread = thread>
void swap(scoped_thread<CallableThread,Thread>& lhs,scoped_thread<CallableThread,Thread>& rhs) noexcept;

  ②使用例子 ^

#include <boost/thread/scoped_thread.hpp>
using namespace boost;
using std::cout;
using std::endl;
int main()
{
    scoped_thread<> st([] {cout << "子线程" << endl; });
    cout << "线程ID:"<<st.get_id() << endl;
    if (st.joinable())
    {
        cout << "可连接的" << endl;
        st.join();
    }
}

  关于线程,目前有很多没搞懂,等理清了补齐例子及注释。

猜你喜欢

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