C++ 多线程 -- Thread

多线程API说明:http://www.cplusplus.com/refe...

两种类型的多任务处理:基于进程和基于线程。

  • 基于进程的多任务处理是程序的并发执行。
  • 基于线程的多任务处理是同一程序的片段的并发执行。

std::thread 在 #include 头文件中声明,因此使用 std::thread 时需要包含 #include 头文件。

包含std::thread类以及std::this_thread命名空间。管理线程的函数和类在 中声明.
< atomic > :包含std::atomic和std::atomic_flag类,以及一套C风格的原子类型和与C兼容的原子操作的函数。

构造函数

  • 默认构造函数
thread() _NOEXCEPT
        {    // construct with no thread
        _Thr_set_null(_Thr);
        }
        
创建一个空的 thread 执行对象。
  • 初始化构造函数
template
explicit thread(Fn&& fn, Args&&... args);
        
创建std::thread执行对象,该thread对象可被joinable,新产生的线程会调用threadFun函数,该函数的参数由 args 给出
  • 拷贝构造函数
thread(const thread&) = delete;

拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。
  • Move构造函数
thread(thread&& x)noexcept

move 构造函数,调用成功之后 x 不代表任何 thread 执行对象。
注意:可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached。

成员函数

  • get_id()

get_id-API说明
获取线程ID,返回类型std::thread::id对象。

inline thread::id thread::get_id() const _NOEXCEPT
    {    // return id for this thread
    return (_Thr_val(_Thr));
    }

Returns the thread id.
返回当前调用的线程ID。

If the thread object is joinable, the function returns a value that uniquely identifies the thread.

If the thread object is not joinable, the function returns a default - constructed object of member type thread::id.
  • joinable

joinable-API说明:判断线程是否可以加入等待

bool joinable() const _NOEXCEPT
        {    // return true if this thread can be joined
        return (!_Thr_is_null(_Thr));
        }

Returns whether the thread object is joinable.
返回线程对象是否是joinable的。

A thread object is joinable if it represents a thread of execution.
如果是一个正在执行的线程,那么它是joinable的。

A thread object is not joinable in any of these cases:
下列任一情况都是非joinable

if it was default-constructed.
默认构造器构造的。
if it has been moved from (either constructing another thread object, or assigning to it).
通过移动构造获得的。
if either of its members join or detach has been called.
调用了join或者detach方法的。
  • join
inline void thread::join()
    {    // join thread
    if (!joinable())
        _Throw_Cpp_error(_INVALID_ARGUMENT);
    const bool _Is_null = _Thr_is_null(_Thr);    // Avoid Clang -Wparentheses-equality
    if (_Is_null)
        _Throw_Cpp_error(_INVALID_ARGUMENT);
    if (get_id() == _STD this_thread::get_id())
        _Throw_Cpp_error(_RESOURCE_DEADLOCK_WOULD_OCCUR);
    if (_Thrd_join(_Thr, 0) != _Thrd_success)
        _Throw_Cpp_error(_NO_SUCH_PROCESS);
    _Thr_set_null(_Thr);
    }
    
The function returns when the thread execution has completed.
当该线程执行完成后才返回。(即等待子线程执行完毕才继续执行主线程)

This synchronizes the moment this function returns with the completion of all  
the operations in the thread: This blocks the execution of the thread that calls  
this function until the function called on construction returns (if it hasn't yet).
该函数的返回与子线程执行完毕同步,该函数会阻塞调用该函数的线程直到子线程调用完毕。

After a call to this function, the thread object becomes non-joinable and can be destroyed safely.
调用该函数后,子线程对象变成non-joinable以及可以安全地销毁。
  • detach
void detach()
        {    // detach thread
        if (!joinable())
            _Throw_Cpp_error(_INVALID_ARGUMENT);
        _Thrd_detachX(_Thr);
        _Thr_set_null(_Thr);
        }
        
Detaches the thread represented by the object from the calling thread, allowing 
them to execute independently from each other.
将本线程从调用线程中分离出来,允许本线程独立执行。(但是当主进程结束的时候,即便是detach()出去的子线程不管有没有完成都会被强制杀死)

Both threads continue without blocking nor synchronizing in any way. Note that when either one ends execution, its resources are released.
两个线程不会堵塞也不会同步,注意他们任一一个结束的时候,所持有的资源将会被释放。

After a call to this function, the thread object becomes non-joinable and can be destroyed safely.
调用该方法后,该线程对象变得不可连接以及可以安全地销毁。
  • swap
  • native_handle
  • hardware_concurrency [static]

包含了与互斥量相关的类以及其他类型和函数

包含两个Provider类(std::promise和std::package_task)和两个Future类(std::future和std::shared_future)以及相关的类型和函数。

包含与条件变量相关的类,包括std::condition_variable和std::condition_variable_any。

线程池

线程同步

https://blog.csdn.net/qwerdf1...

异步

线程管理(一个线程管理其他线程)

你可能感兴趣的:(c++,多线程,c++11)