std::thread::thread

thread() noexcept;
(1) (C++11 起)
thread( thread&& other ) noexcept;
(2) (C++11 起)
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
(3) (C++11 起)
thread(const thread&) = delete;
(4) (C++11 起)

构造新的 thread 对象。

1) 构造不表示线程的新 thread 对象。
2) 移动构造函数。构造表示曾为 other 所表示的执行线程的 thread 对象。此调用后 other 不再表示执行线程。
3) 构造新的 std::thread 对象并将它与执行线程关联。新的执行线程开始执行
std::invoke(decay_copy(std::forward<Function>(f)), decay_copy(std::forward<Args>(args))...);

其中 decay_copy 定义为

template <class T>
std::decay_t<T> decay_copy(T&& v) { return std::forward<T>(v); }
除了 decay_copy 的调用在调用方语境求值,故而任何求值和复制/移动参数中抛出的异常被抛到当前线程,而不用开始新线程。
构造函数的调用完成同步于(定义于 std::memory_order )新的执行线程上 f 副本的调用开始。
std::decay_t<Function> 是与 std::thread 相同的类型,则此构造函数不参与重载决议。
(C++14 起)
4) 复制构造函数被删除; thread 不可复制。没有二个 std::thread 对象可表示同一执行线程。

目录

参数

other - 用以构造此 thread 的另一 thread 对象
f - 执行于新线程的可调用对象 (Callable)
args... - 传递给新函数的参数

后置条件

1) get_id() 等于 std::thread::id() (即 joinablefalse
2) other.get_id() 等于 std::thread::id()get_id() 返回构造开始前 other.get_id() 的值
3) get_id() 不等于 std::thread::id() (即 joinabletrue

异常

3) 若不能开始线程则抛出 std::system_error 。异常可能表示错误条件 std::errc::resource_unavailable_try_again 或另一实现限定的错误条件。

注意

到线程函数的参数被移动或按值复制。若需要传递引用参数给线程函数,则必须包装它(例如用 std::refstd::cref )。

忽略来自函数的任何返回值。若函数抛异常,则调用 std::terminate 。为将返回值或异常传递回调用方线程,可使用 std::promisestd::async

示例

#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>
 
void f1(int n)
{
    for (int i = 0; i < 5; ++i) {
        std::cout << "Thread 1 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}
 
void f2(int& n)
{
    for (int i = 0; i < 5; ++i) {
        std::cout << "Thread 2 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}
 
int main()
{
    int n = 0;
    std::thread t1; // t1 非线程
    std::thread t2(f1, n + 1); // 按值传递
    std::thread t3(f2, std::ref(n)); // 按引用传递
    std::thread t4(std::move(t3)); // t4 现在运行 f2() 。 t3 不再是线程
    t2.join();
    t4.join();
    std::cout << "Final value of n is " << n << '\n';
}

可能的输出:

Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 2 executing
Thread 1 executing
Final value of n is 5

引用

  • 30.3.1.2 thread constructors [thread.thread.constr]

参阅

thrd_createC 文档

版本历史

  • (当前 | 先前 2017年8月25日 (五) 06:45Fruderica讨论 | 贡献. . (4,083字节) (-1,409). . (撤销)
  • 当前 | 先前 2013年7月9日 (二) 02:08P12bot讨论 | 贡献 . . (5,492字节) (+6). . (Allow search engines to index popular pages.) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:36P12bot讨论 | 贡献 . . (5,486字节) (-154). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 18:16P12bot讨论 | 贡献 . . (5,640字节) (+257). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 03:32P12讨论 | 贡献 . . (5,383字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (5,383字节) (+5,383). . (Translated from the English version using Google Translate)