官方文档:

windows的Mutex:

​https://docs.microsoft.com/zh-cn/windows/win32/api/synchapi/nf-synchapi-createmutexa?redirectedfrom=MSDN&f1url=%3FappId%3DDev15IDEF1%26l%3DZH-CN%26k%3Dk(SYNCHAPI%252FCreateMutex);k(CreateMutex);k(DevLang-C%252B%252B);k(TargetOS-Windows)%26rd%3Dtrue​

c++11:

​https://en.cppreference.com/w/cpp/thread​

代码:

// ConsoleApplication10.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <windows.h>
#include <mutex>

int main()
{
/* WINDOWS MUTEX, unlock 一个没锁上的锁不会有任何异常
HANDLE h_m = CreateMutex(NULL, FALSE, NULL);
ReleaseMutex(h_m);

std::cout << "Hello World 1 !\n";

ReleaseMutex(h_m);

std::cout << "Hello World 2 !\n";


/* c++11 MUTEX, unlock 一个没锁上的锁报异常
std::mutex mu;
mu.unlock();

std::cout << "Hello World 1 !\n";

mu.unlock();

std::cout << "Hello World 2 !\n";
*/

/* c++11 MUTEX, double lock 一个锁,报异常
std::mutex mu;
mu.lock();

std::cout << "Hello World 1 !\n";

mu.lock();

std::cout << "Hello World 2 !\n";
*/

/* c++11 recuisive_mutex, double lock 一个锁,正常
std::recursive_mutex mu;
mu.lock();

std::cout << "Hello World 1 !\n";

mu.lock();

std::cout << "Hello World 2 !\n";
*/

/* c++11 recuisive_mutex, unlock 一个没加锁的锁, 异常*/
std::recursive_mutex mu;
mu.unlock();

std::cout << "Hello World 1 !\n";

mu.unlock();

std::cout << "Hello World 2 !\n";


while (1) {
;
}


}

截图: 

[C++] Windows下Mutex 与 c++ 11 的 mutex 和 recuisive_mutex 的区别_hapi

[C++] Windows下Mutex 与 c++ 11 的 mutex 和 recuisive_mutex 的区别_hapi_02