C++ concurrency and mutual exclusion protection example

        Recently, I want to write a multi-threaded concurrent database. I mainly hope to use read-write locks to achieve concurrent access to the library. At the same time, considering that other platforms (such as Iar) do not have C++ read-write locks and need to be provided by the operating system, I encapsulate the read-write locks. stand up. The whole process was quite tortuous and encountered many problems. Here I will briefly analyze and summarize concurrency and mutual exclusion.

        First, paste part of the source code:

#include <shared_mutex>
#include <iostream>
#include <windows.h>
#include <synchapi.h>

using cegn_mutex = std::shared_mutex;
cegn_mutex g_cegn_mutex;
void cegn_mutex_unique_lck(cegn_mutex& testmutex)	//独占锁,写数据
{
	std::unique_lock<cegn_mutex> cegn_lock(testmutex);
}

void cegn_mutex_share_lck(cegn_mutex& Dbmutex)	//共享锁,读数据
{
	std::shared_lock<cegn_mutex> cegn_lock(Dbmutex);
}

void cegn_mutex_unlck(cegn_mutex& Dbmutex)
{
	;	//vc读写锁离开作用域自动释放
}

int g_dwVal = 0;
void FastWriteData(int i)
{
	while (1)
	{
		cegn_mutex_unique_lck(g_cegn_mutex);
		g_dwVal++;
		std::cout << "FastWriteData " << "  Set dwVal= " << g_dwVal << "\n";

		Sleep(1000);
		cegn_mutex_unlck(g_cegn_mutex);
	}
}

void SlowWriteData(int i)
{
	while (1)
	{
		cegn_mutex_unique_lck(g_cegn_mutex);
		g_dwVal++;
		std::cout << "SlowWriteData " << " Set dwVal= " << g_dwVal << "\n";
		Sleep(5000);
		cegn_mutex_unlck(g_cegn_mutex);
	}
}

void ReadData(int i)
{
	while (1)
	{
		cegn_mutex_share_lck(g_cegn_mutex);
		std::cout << "ReadData " << " Get dwVal= " << g_dwVal << "\n";
		Sleep(500);
		cegn_mutex_unlck(g_cegn_mutex);
	}
}

int main()
{
	std::cout << "main start !!" << std::endl;

	std::thread thread1 = std::thread(FastWriteData, 0);
	std::thread thread2 = std::thread(SlowWriteData, 0);
	thread1.join();
	thread2.join();

	getchar();
	return 1;
}

The code is not long and the logic is quite clear, but the result is incorrect:

There seems to be no mutual exclusion protection, because both FastWriteData and SlowWriteData exclusively occupy cegn_mutex_unique_lck(g_cegn_mutex);

And in while(1), there is no situation of releasing the write lock, so the two writing threads should not appear alternately.

Let chatgpt analyze the above and it thinks there is no problem. I tried to modify it back to the standard read-write lock interface, as follows:

void FastWriteData(int i)
{
	while (1)
	{
//		cegn_mutex_unique_lck(g_cegn_mutex);
		std::unique_lock<cegn_mutex> lck(g_cegn_mutex);
		g_dwVal++;
		std::cout << "FastWriteData " << "  Set dwVal= " << g_dwVal << "\n";

		Sleep(1000);
		cegn_mutex_unlck(g_cegn_mutex);
	}
}

void SlowWriteData(int i)
{
	while (1)
	{
//		cegn_mutex_unique_lck(g_cegn_mutex);
		std::unique_lock<cegn_mutex> lck(g_cegn_mutex);
		g_dwVal++;
		std::cout << "SlowWriteData " << " Set dwVal= " << g_dwVal << "\n";
		Sleep(5000);
		cegn_mutex_unlck(g_cegn_mutex);
	}
}

 As above, the code runs normally.

main start !!
FastWriteData   Set dwVal= 1
FastWriteData   Set dwVal= 2
FastWriteData   Set dwVal= 3
FastWriteData   Set dwVal= 4
FastWriteData   Set dwVal= 5
FastWriteData   Set dwVal= 6
FastWriteData   Set dwVal= 7
FastWriteData   Set dwVal= 8
FastWriteData   Set dwVal= 9
FastWriteData   Set dwVal= 10
FastWriteData   Set dwVal= 11
FastWriteData   Set dwVal= 12
FastWriteData   Set dwVal= 13
FastWriteData   Set dwVal= 14

Now FastWriteData exclusively occupies the mutex, causing SlowWriteData to be unable to run. Why use interface:

void cegn_mutex_unique_lck(cegn_mutex& testmutex) //Exclusive lock, write data
{     std::unique_lock<cegn_mutex> cegn_lock(testmutex); }

That won’t work?

Modify to call directly:

using cegn_mutex = std::shared_mutex;
cegn_mutex g_cegn_mutex;
void cegn_mutex_unique_lck(cegn_mutex& testmutex)	//独占锁,写数据
{
//	std::unique_lock<cegn_mutex> cegn_lock(testmutex);
	std::unique_lock<cegn_mutex> cegn_lock(g_cegn_mutex);
}

It still cannot be mutually exclusive correctly, and the same is true if it is modified as follows:

void cegn_mutex_unique_lck(cegn_mutex& testmutex)	//独占锁,写数据
{
//	std::unique_lock<cegn_mutex> cegn_lock(testmutex);
	std::unique_lock<std::shared_mutex> cegn_lock(g_cegn_mutex);
}

After analysis, the problem is:

void cegn_mutex_unique_lck(cegn_mutex& testmutex)

A mutex cegn_lock is defined in the function:

std::unique_lock<cegn_mutex> cegn_lock(testmutex);

The mutex's life cycle ends when the function exits, so it is automatically destroyed, which ultimately leads to the inability to be mutually exclusive. That is because you want to encapsulate it. How to achieve it? You can agree on a class encapsulation yourself:

The complete simple code is as follows:

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

class MutexWrapper {
public:
	MutexWrapper(std::mutex& mutex) : m_mutex(mutex) {
		m_mutex.lock();
	}

	~MutexWrapper() {
		m_mutex.unlock();
	}

private:
	std::mutex& m_mutex;
};

std::mutex g_mutex_test;
int g_dwVal = 0;

void FastWriteData(int i) {
	while (1) {
		MutexWrapper lock(g_mutex_test);
		g_dwVal++;
		std::cout << "FastWriteData " << "  Set dwVal= " << g_dwVal << "\n";
		Sleep(1000);
	}
}

void SlowWriteData(int i) {
	while (1) {
		MutexWrapper lock(g_mutex_test);
		g_dwVal++;
		std::cout << "SlowWriteData " << " Set dwVal= " << g_dwVal << "\n";
		Sleep(3000);
	}
}

int main() {
	std::cout << "main start !!" << std::endl;

	std::thread thread1 = std::thread(FastWriteData, 0);
	std::thread thread2 = std::thread(SlowWriteData, 0);
	thread1.join();
	thread2.join();

	getchar();
	return 1;
}

So, it runs normally

Modify the routine so that both processes run in full line 

void FastWriteData(int i) {
	while (1) {
		{
			MutexWrapper lock(g_mutex_test);
			g_dwVal++;
			std::cout << "FastWriteData " << "  Set dwVal= " << g_dwVal << "\n";
		}
		Sleep(1000);
	}
}

void SlowWriteData(int i) {
	while (1) {
		{
			MutexWrapper lock(g_mutex_test);
			g_dwVal++;
			std::cout << "SlowWriteData " << " Set dwVal= " << g_dwVal << "\n";
		}
		Sleep(3000);
	}
}

As above, the code is basically normal.

Of course, the mutex lock can also be modified into a read-write lock, as follows:

class MutexWrapper {
public:
	MutexWrapper(std::shared_mutex& mutex) : m_mutex(mutex) {
		m_mutex.lock();
	}

	~MutexWrapper() {
		m_mutex.unlock();
	}

private:
	std::shared_mutex& m_mutex;
};

std::shared_mutex g_mutex_test;

The code also runs normally.

To sum up:

1: Based on RAII, many variables in C++ have limited life cycles, so special attention must be paid to the life cycles of smart variables.

2: If you need to encapsulate the read-write lock, you can't simply package it by function. If it doesn't work, just use a class to encapsulate it.

3: Be proficient in the usage of std::thread, std::shared_mutex, and std::mutex. This is the basic requirement for mutual exclusion of changes.

Guess you like

Origin blog.csdn.net/weixin_45119096/article/details/132367642