参考引用
- C++11 14 17 20 多线程从原理到线程池实战
- 代码运行环境:Visual Studio 2019

避免竞争状态策略,对临界区进行保护,同时只能有一个线程进入临界区
#include
#include
#include
#include
using namespace std;
static mutex mux;
void TestThread() {
for (;;)
{
// 获取锁资源,如果没有则阻塞等待(一次只能有一个线程拿到锁)
// 拿锁的原则:尽晚申请、尽早释放
//mux.lock(); // 拿锁方式一
if (!mux.try_lock()) { // 拿锁方式二:可以看到多个进程在竞争拿锁的情况
cout << "." << flush;
this_thread::sleep_for(100ms);
continue;
}
// 业务代码
cout << "=========" << endl;
cout << "Test 001" << endl;
cout << "Test 002" << endl;
cout << "Test 003" << endl;
cout << "=========" << endl;
mux.unlock(); // 如果忘记释放锁,则会导致死锁,所有线程都在等待
this_thread::sleep_for(1000ms);
}
}
int main(int argc, char* argv[]) {
// 同时创建 10 个线程
for (int i = 0; i < 10; i++) {
thread th(TestThread);
th.detach();
}
getchar();
return 0;
}
#include
#include
#include
#include
using namespace std;
static mutex mux;
void ThreadMainMux(int i) {
for (;;)
{
mux.lock();
cout << i << "[in]" << endl;
this_thread::sleep_for(1000ms);
mux.unlock();
// 防止 unlock() 还未释放完全就进入下一个 lock(),导致其他线程拿不到锁
this_thread::sleep_for(1ms);
}
}
int main(int argc, char* argv[]) {
// 同时创建 3 个线程
for (int i = 0; i < 3; i++) {
thread th(ThreadMainMux, i + 1);
th.detach();
}
getchar();
return 0;
}
#include
#include
#include
#include
using namespace std;
timed_mutex tmux; // 支持超时的互斥锁
void ThreadMainTime(int i) {
for (;;)
{
if (!tmux.try_lock_for(chrono::milliseconds(500))) {
cout << i << "[try_lock_for timeout]" << endl;
continue;
}
cout << i << "[in]" << endl;
this_thread::sleep_for(2000ms);
tmux.unlock();
// 防止 unlock() 还未释放完全就进入下一个 lock(),导致其他线程拿不到锁
this_thread::sleep_for(1ms);
}
}
int main(int argc, char* argv[]) {
getchar();
// 同时创建 3 个线程
for (int i = 0; i < 3; i++) {
thread th(ThreadMainTime, i + 1);
th.detach();
}
getchar();
return 0;
}
#include
#include
#include
#include
using namespace std;
recursive_mutex rmux; // 支持可重入的互斥锁
void Task1() {
rmux.lock();
cout << "task1 [in]" << endl;
rmux.unlock();
}
void Task2() {
rmux.lock();
cout << "task2 [in]" << endl;
rmux.unlock();
}
void ThreadMainRec(int i) {
for (;;)
{
// 加锁几次对应的也要解锁几次
rmux.lock();
Task1();
cout << i << "[in]" << endl;
this_thread::sleep_for(2000ms);
Task2();
rmux.unlock();
this_thread::sleep_for(1ms);
}
}
int main(int argc, char* argv[]) {
// 同时创建 3 个线程
for (int i = 0; i < 3; i++) {
thread th(ThreadMainRec, i + 1);
th.detach();
}
getchar();
return 0;
}

#include
#include
#include
#include
#include
using namespace std;
shared_timed_mutex stmux; // 支持可重入的共享锁 C++14
// 读取线程
void ThreadRead(int i) {
for (;;)
{
stmux.lock_shared();
cout << i << " Read" << endl;
this_thread::sleep_for(500ms);
stmux.unlock_shared();
this_thread::sleep_for(1ms);
}
}
// 写入线程
void ThreadWrite(int i) {
for (;;)
{
stmux.lock_shared(); // 只要没有锁定互斥锁,共享锁都是立即返回
// 读取数据
stmux.unlock_shared();
// 互斥锁 写入(同时只能一个线程写入),共享锁和互斥锁都不能进入
stmux.lock();
cout << i << " Write" << endl;
this_thread::sleep_for(300ms);
stmux.unlock();
this_thread::sleep_for(1ms);
}
}
int main(int argc, char* argv[]) {
for (int i = 0; i < 3; i++) {
thread th(ThreadWrite, i + 1);
th.detach();
}
for (int i = 0; i < 3; i++) {
thread th(ThreadRead, i + 1);
th.detach();
}
getchar();
return 0;
}