Namespaces
Variants

std::thread:: ~thread

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
~thread ( ) ;
(C++11 起)

销毁线程对象。

* this 关联有执行线程( joinable ( ) == true ),则将调用 std:: terminate ( )

注释

当线程对象满足以下条件时,它不再关联实际执行线程(此时销毁是安全的):

  • 它是默认构造的。
  • 它已被移动。
  • join() 已被调用。
  • detach() 已被调用。

示例

#include <thread>
using namespace std::chrono_literals;
int main()
{
    auto bleah = std::thread{[]{ std::this_thread::sleep_for(13ms); }};
}   // ~thread calls std::terminate()

可能的输出:

terminate called without an active exception

另请参阅

若线程可加入,则请求停止并等待线程结束
( std::jthread 的公开成员函数)