Boost C++ 库

...世界上最受推崇和专业设计的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu, C++ 编码标准

PrevUpHomeNext

作用域线程

动机
教程
自由线程仿函数
strict_scoped_thread
scoped_thread
非成员函数 swap(scoped_thread&,scoped_thread&)

概要

//#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;

基于《C++ 并发编程实战》中定义的 scoped_thread 类,Boost.Thread 定义了一个线程包装器类,该类在销毁时如果线程可连接,则调用作为模板参数给定的特定操作,而不是调用 terminate。

虽然《C++ 并发编程实战》中定义的 scoped_thread 类更接近于不允许对包装线程进行任何更改的 strict_scoped_thread 类,但 Boost.Thread 提供了一个 scoped_thread 类,该类提供了与 thread 相同的非弃用接口。

作用域线程是线程的包装器,允许用户声明在销毁时执行什么操作。常见的用途之一是在销毁时连接线程,因此这是默认行为。这是与线程的唯一区别。当线程在析构函数中调用 std::terminate() 如果线程是可连接的,strict_scoped_thread<> 或 scoped_thread<> 会在可连接时连接线程。

strict_scoped_thread 和 scoped_thread 之间的区别在于 strict_scoped_thread 完全隐藏了拥有的线程,因此用户除了作为参数给出的特定操作外,无法对拥有的线程执行任何操作,而 scoped_thread 提供了与 thread 相同的接口并转发所有操作。

boost::strict_scoped_thread<> t1((boost::thread(f)));
//t1.detach(); // compile fails
boost::scoped_thread<> t2((boost::thread(f)));
t2.detach();
//#include <boost/thread/scoped_thread.hpp>

struct detach;
struct join_if_joinable;
struct interrupt_and_join_if_joinable;
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();
    }
  }
};
// #include <boost/thread/scoped_thread.hpp>

template <class CallableThread = join_if_joinable, class Thread = ::boost::thread>
class strict_scoped_thread
{
  thread t_; // for exposition purposes only
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();

};

RAII thread 包装器,添加了特定的析构器,允许掌握在销毁时可以执行的操作。

CallableThread:一个可调用的 void(thread&)

默认值为 join_if_joinable

如果 thread 可连接,线程析构函数将终止程序。此包装器可用于在销毁线程之前连接线程。

示例
boost::strict_scoped_thread<> t((boost::thread(F)));

thread 构造

explicit strict_scoped_thread(Thread&& t) noexcept;

效果

将线程移动到拥有的 t_

抛出

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

效果

就地构造一个内部线程。

后置条件

*this.t_ 指的是新创建的执行线程,并且 this->get_id()!=thread::id()

抛出

线程构造可能抛出的任何异常。

~strict_scoped_thread();

效果

等效于 CallableThread()(t_)

抛出

无:CallableThread()(t_) 在连接线程时不应抛出异常,因为作用域变量在线程函数之外的作用域中。

#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();

    // move support
    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;

RAII thread 包装器,添加了特定的析构器,允许掌握在销毁时可以执行的操作。

CallableThread:一个可调用的 void(thread&)。默认值为 join_if_joinable。

如果线程可连接,线程析构函数将终止程序。此包装器可用于在销毁线程之前连接线程。

备注:scoped_thread 不是 thread,因为 thread 没有被设计为作为多态类型派生。

无论如何,scoped_thread 可以在大多数可以使用 thread 的上下文中使用,因为它具有相同的非弃用接口,除了构造函数。

示例
boost::scoped_thread<> t((boost::thread(F)));
t.interrupt();
scoped_thread() noexcept;

效果

构造一个包装到 Not-a-Thread 的 scoped_thread 实例。

后置条件

this->get_id()==thread::id()

抛出

scoped_thread(scoped_thread&& other) noexcept;

效果

将由 other 管理的 scoped_thread 的所有权(如果有)转移到新构造的 scoped_thread 实例。

后置条件

other.get_id()==thread::id() 并且 get_id() 返回构造之前的 other.get_id() 的值

抛出

scoped_thread& operator=(scoped_thread&& other) noexcept;

效果

在调用 CallableThread()(t_) 之后,将由 other 管理的 scoped_thread 的所有权(如果有)转移到 *this

后置条件

other->get_id()==thread::id() 并且 get_id() 返回赋值之前的 other.get_id() 的值。

抛出

无:CallableThread()(t_) 在连接线程时不应抛出异常,因为作用域变量在线程函数之外的作用域中。

thread 移动构造函数

scoped_thread(thread&& t);

效果

将由 other 管理的线程的所有权(如果有)转移到新构造的 scoped_thread 实例。

后置条件

other.get_id()==thread::id() 并且 get_id() 返回构造之前的 other.get_id() 的值。

抛出

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

效果

就地构造一个内部线程。

后置条件

*this.t_ 指的是新创建的执行线程,并且 this->get_id()!=thread::id()

抛出

线程构造可能抛出的任何异常。

~scoped_thread();

效果

等效于 CallableThread()(t_)

抛出

无:CallableThread()(t_) 在连接线程时不应抛出异常,因为作用域变量在线程函数之外的作用域中。

bool joinable() const noexcept;

返回值

等效于 return t_.joinable()。

抛出

void join();

效果

等效于 t_.join()。

template <class Rep, class Period>
bool try_join_for(const chrono::duration<Rep, Period>& rel_time);

效果

等效于 return t_.try_join_for(rel_time)

template <class Clock, class Duration>
bool try_join_until(const chrono::time_point<Clock, Duration>& abs_time);

效果

等效于 return t_.try_join_until(abs_time)

void detach();

效果

等效于 t_.detach()

thread::id get_id() const noexcept;

效果

等效于 return t_.get_id()

void interrupt();

效果

等效于 t_.interrupt()

unsigned hardware_concurrency() noexecpt;

效果

等效于 return thread::hardware_concurrency()

unsigned physical_concurrency() noexecpt;

效果

等效于 return thread::physical_concurrency()

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

效果

等效于 return t_.native_handle()

void swap(scoped_thread& other) noexcept;

效果

等效于 t_.swap(other.t_)

#include <boost/thread/scoped_thread.hpp>

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

效果

lhs.swap(rhs).


PrevUpHomeNext