detach(); std::this_thread::sleep_for(std::chrono::seconds(1)); std::cout << "Exiting thread caller.\n"; } int main() { threadCaller(); std::this_thread::sleep_for(std::chrono::seconds(5)); } Possible output:
thrd_detach From cppreference.com <c |thread Defined in header<threads.h> intthrd_detach(thrd_tthr); (since C11) Detaches the thread identified bythrfrom the current environment. The resources held by the thread will be freed automatically once the thread exits....
detach permits the thread to execute independently from the thread handle (public member function) swap swaps two thread objects (public member function) Non-member functions std::swap(std::thread) (C++11) specializes thestd::swapalgorithm
thread::join thread::detach thread::swap Non-member functions swap(std::thread)void swap( std::thread& lhs, std::thread& rhs ) noexcept; (since C++11) Overloads the std::swap algorithm for std::thread. Exchanges the state of lhs with that of rhs. Effectively calls lhs.swap(rhs). ...
thread−jthread(C++20) atomic−atomic_flag atomic_ref(C++20)−memory_order Mutual exclusion−Condition variables Futures−Semaphores(C++20) latch(C++20)−barrier(C++20) Safe Reclamation(C++26) Execution support library(C++26) Feature test macros(C++20) ...
detach(); this_thread // 获取当前线程ID std::this_thread::get_id(); // 使当前线程休眠一段指定时间 std::this_thread::sleep_for(); // 使当前线程休眠到指定时间 std::this_thread::sleep_until(); // 暂停当前线程的执行,让别的线程执行 std::this_thread::yield(); ...
thread my_thread; // 阻塞 my_thread.join(); // 非阻塞 my_thread.detach(); this_thread // 获取当前线程ID std::this_thread::get_id(); // 使当前线程休眠一段指定时间 std::this_thread::sleep_for(); // 使当前线程休眠到指定时间 std::this_thread::sleep_until(); // 暂停当前线程的...
detach(); this_thread // 获取当前线程ID std::this_thread::get_id(); // 使当前线程休眠一段指定时间 std::this_thread::sleep_for(); // 使当前线程休眠到指定时间 std::this_thread::sleep_until(); // 暂停当前线程的执行,让别的线程执行 std::this_thread::yield(); 锁 #include <...
detach(); this_thread // 获取当前线程ID std::this_thread::get_id(); // 使当前线程休眠一段指定时间 std::this_thread::sleep_for(); // 使当前线程休眠到指定时间 std::this_thread::sleep_until(); // 暂停当前线程的执行,让别的线程执行 std::this_thread::yield(); ...
detach(); this_thread // 获取当前线程ID std::this_thread::get_id(); // 使当前线程休眠一段指定时间 std::this_thread::sleep_for(); // 使当前线程休眠到指定时间 std::this_thread::sleep_until(); // 暂停当前线程的执行,让别的线程执行 std::this_thread::yield(); 锁 #include <...