#include <thread>
std::thread first (foo, arg);
first.join();
// terminate thread
// call destructor befor join() or detach()
// https://stackoverflow.com/questions/12207684/how-do-i-terminate-a-thread-in-c11
std::thread* second = new std::thread (foo, arg);
delete second;
#include <thread>
#include <chrono>
std::this_thread::sleep_for(std::chrono::seconds(1));
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::ostringstream ss;
ss << std::this_thread::get_id();
std::string idstr = ss.str();
#include <mutex>
std::mutex mtx;
mtx.lock();
//...
mtx.unlock();
不可重入,如果重入则会抛出 std::system_error
异常
std::lock_guard<std::mutex> lk(io_mutex);
递归锁,被锁定时,允许统一线程重复锁定,但是阻止其他线程锁定。