关于c++:C-多线程-Thread

9次阅读

共计 3373 个字符,预计需要花费 9 分钟才能阅读完成。

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

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

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

<thread>

std::thread 在 #include<thread> 头文件中申明,因而应用 std::thread 时须要蕴含 #include<thread> 头文件。

蕴含 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<class Fn, class... Args>
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]

<atomic>

<mutex>

蕴含了与互斥量相干的类以及其余类型和函数

<future>

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

<condition_variable>

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

线程池

线程同步

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

异步

线程治理(一个线程治理其余线程)

正文完
 0