Missmiaom
5/7/2020 - 3:44 AM

thread

thread

#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;

sleep

#include <thread>
#include <chrono>

std::this_thread::sleep_for(std::chrono::seconds(1));
std::this_thread::sleep_for(std::chrono::milliseconds(10));

thread id

std::ostringstream ss;
ss << std::this_thread::get_id();
std::string idstr = ss.str();

mutex

#include <mutex>

std::mutex mtx;
mtx.lock();
//...
mtx.unlock();

std::mutex

不可重入,如果重入则会抛出 std::system_error 异常

lock_guard

std::lock_guard<std::mutex> lk(io_mutex);

recursive_mutex

递归锁,被锁定时,允许统一线程重复锁定,但是阻止其他线程锁定。