具有两个独占锁组的共享锁

Shared lock with two exclusive lock groups

本文关键字:共享锁 两个      更新时间:2023-10-16

我有两个方法"log"和"measure",不应该同时执行。 所以我尝试使用"std::mutex"来执行此操作,如下所示:

void log(std::string message)
{
mtx.lock();
someLogFunctionality();
mtx.unlock();
}
void measure()
{        
mtx.lock();
someMeasureFunctionality();
mtx.unlock();
}

现在事实证明,也可以在不锁定的情况下并行多次调用"log",这同样适用于"度量"。(原因:someLogFunction(( 和 someMeasureFunction(( 相互干扰,但相同的方法可以多次并行调用(

我当时看了一下"std::shared_mutex",但对我来说有两个问题:

1.( 有了shared_mutex我只能将lock_shared用于其中一种方法(日志或测量(,但另一种方法必须使用独占锁(并且不能并行执行多次(

void log(std::string message)
{
mtx.lock_shared();
someLogFunctionality();
mtx.unlock_shared();
}
void measure()
{        
mtx.lock(); // This should also be shared but among another "group"
someMeasureFunctionality();
mtx.unlock();
}

2.( 我不能使用 C++17(我正在使用的环境中的约束(

你对我如何实现这一点有什么建议吗?

根据 alexb 的回复,我编写了以下互斥类,该类目前对我有用(到目前为止仅在一个简单的多线程示例应用程序中试用过(

请注意,它不受"饥饿"保护。简单来说:如果 lockLogging 被频繁调用(反之亦然(,则无法确保 lockMeasure 将永远获得锁。

class MyMutex
{
private:
std::atomic<int> log_executors;
std::atomic<int> measure_executors;
std::mutex mtx;
std::condition_variable condition;
public:
MyMutex() : log_executors(0), measure_executors(0) {}

~MyMutex() {}
void lockMeasure()
{   
std::unique_lock<std::mutex> lock(mtx);
while(log_executors) {
condition.wait(lock); 
}
measure_executors++; 
}

void unlockMeasure()
{   
std::unique_lock<std::mutex> lock(mtx);
measure_executors--; 
if (!measure_executors)
{
condition.notify_all();
}
}

void lockLogging()
{         
std::unique_lock<std::mutex> lock(mtx);
while(measure_executors) {
condition.wait(lock); 
}
log_executors++;
}
void unlockLogging()
{         
std::unique_lock<std::mutex> lock(mtx);
log_executors--; 
if (!log_executors)
{
condition.notify_all(); 
}
}
static MyMutex& getInstance()
{
static MyMutex _instance;
return _instance;
}    
};

用法:

void measure()
{
MyMutex::getInstance().lockMeasure();
someMeasureFunctionality();
MyMutex::getInstance().unlockMeasure();
}
void log()
{
MyMutex::getInstance().lockLogging();
someLogFunctionality();
MyMutex::getInstance().unlockLogging();
}

你需要一些比shared_mutex更复杂的屏障逻辑(顺便说一句,shared_mutex不是多平台编译的最佳选择(。例如,您可以使用互斥锁、条件变量和 2 个变量进行屏障同步。它不需要 CPU,您不能使用睡眠进行检查。

#include <mutex>
#include <condition_variable>
#include <atomic>
std::atomic<int> log_executors = 0;
std::atomic<int> measure_executors = 0;
std::mutex mutex;
std::condition_variable condition;
void log(std::string message) {
{
std::unique_lock<std::mutex> lock(mutex);
log_executors++;  // Register current executor and prevent from entering new measure executors
// Wait until all measure executors will go away
while(measure_executors) {
condition.wait(lock);  // wait condition variable signal. Mutex will be unlocked during wait
}
}
// here lock is freed
someLogFunctionality(); // execute logic

{
std::unique_lock<std::mutex> lock(mutex);
log_executors--;  // unregister current execution
condition.notify_all();  // send signal and unlock all waiters
}
}
void measure()
{        
{
std::unique_lock<std::mutex> lock(mutex);
measure_executors++;  // Register current executor and prevent from entering new log executors
while(log_executors) {
condition.wait(lock);  // wait until all measure executors will gone
}
}
someMeasureFunctionality();
{
std::unique_lock<std::mutex> lock(mutex);
measure_executors--;  // unregister current execution
condition.notify_all(); // send signal and unlock all waiters
}
}

您可以使用主锁授予对信号量变量的访问权限:

void log(std::string message)
{
acquire(LOG);
someLogFunctionality();
release(LOG);
}
void measure()
{        
acquire(MEASURE);
someMeasureFunctionality();
release(MEASURE);
}
void acquire(int what) {
for (;;) {
mtx.lock();
if (owner == NONE) {
owner = what;
}
if (owner == what) {
// A LOG was asked while LOG is running
users[owner]++;
mtx.unlock();
return;
}
mtx.unlock();
// Some sleep would be good
usleep(5000);
}
}
void release(int what) {
mtx.lock();
if (owner != what) {
// This is an error. How could this happen?
}
if (users[what] <= 0) {
// This is an error. How could this happen?
}
users[what]--;
if (0 == users[what]) {
owner = NONE;
}
mtx.unlock();
}

在这种情况下,例如:

owner is NONE
LOG1 acquires LOG. It can do so because owner is NONE
MEASURE1 acquires LOG. It starts spinning in place because owner != MEASURE
MEASURE2 acquires LOG. It starts spinning in place because owner != MEASURE
LOG2 acquires LOG. It can do so because owner is LOG, users[LOG]=2
LOG2 releases LOG. users[LOG]=1
LOG1 releases LOG. users[LOG]=0, so owner becomes NONE
MEASURE2 by pure chance acquires mtx before MEASURE1, finds owner=NONE and goes
MEASURE1 finds owner=MEASURE and sets users[MEASURE]=2

在上面,请注意对 measure(( 的第二次调用实际上执行得更早一些。这应该没问题。但是,如果你想保持调用"序列化",即使它们是并行发生的,你将需要为每个所有者提供一个堆栈和更复杂的代码。