Based on multi-thread concurrency-thread synchronization-system implementation

System implementation: Compared with STL, it is a non-standard implementation, and the implementation of Linux and Windows platforms.
Thread synchronization: Protects resources by restricting multiple threads from executing a certain piece of code at the same time.

One, linux mutex

1. Initialization of the thread mutex pthread_mutex_t

a. Define reinitialization:

pthread_mutex_t mymutex;//定义互斥体
pthread_mutex_init(&mymutex, NULL);// 初始化

The second parameter attr of the pthread_mutex_init function is the attribute that defines the mutex, which is generally NULL. Returns 0 on successful initialization, otherwise returns something else (error number).

The attributes of the mutex are specified when the lock is created. There is only one lock type attribute in the LinuxThreads implementation. Different lock types behave differently when trying to lock an already locked mutex. Currently (glibc2.2.3, linuxthreads0.9) there are four values ​​to choose from:

  • PTHREAD_MUTEX_TIMED_NP, this is the default value, which is a normal lock. When a thread is locked, the rest of the threads requesting the lock will form a waiting queue, and obtain the lock according to the priority after unlocking. This lock strategy ensures the fairness of resource allocation.

  • PTHREAD_MUTEX_RECURSIVE_NP, nested lock, allows the same thread to successfully acquire the same lock multiple times, and unlock it through multiple unlocks. If it is a request from a different thread, it will re-compete when the locking thread is unlocked.

  • PTHREAD_MUTEX_ERRORCHECK_NP, error detection lock, if the same thread requests the same lock, return EDEADLK, otherwise it is the same as PTHREAD_MUTEX_TIMED_NP type action. This ensures that deadlock in the simplest case does not occur when multiple locks are not allowed.

  • PTHREAD_MUTEX_ADAPTIVE_NP, adaptive lock, the simplest lock type, only waits for unlocking and then re-competes.

b. Define simultaneous initialization

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

2. Acquire lock
a, block interface

int ret = pthread_mutex_lock(&mymutex);// 阻塞锁定,

Suspend waiting when the lock is already held.

b. Non-blocking interface

int ret = pthread_mutex_trylock(&mymutex);//非阻塞式锁定,

Returns EBUSY when the lock is already held, rather than hanging and waiting.
3. Unlock

ret = pthread_mutex_unlock(&mymutex);// 解锁

4. Destroy

ret = pthread_mutex_destroy(&mymutex);// 销毁

5. Example of use

#include<pthread.h>
int main()
{
    
    
	pthread_mutex_t mymutex;//定义互斥体
	pthread_mutex_init(&mymutex, NULL);// 初始化
	int ret = pthread_mutex_lock(&mymutex);// 阻塞锁定,
	//int ret = pthread_mutex_trylock(&mymutex);//非阻塞式锁定,
	ret = pthread_mutex_unlock(&mymutex);// 解锁
	ret = pthread_mutex_destroy(&mymutex);// 销毁
}

Two, Windows critical section (critical section)

//创建:
	CRITICAL_SECTION my_winsec;//创建windows中的临界区,类似与互斥量,使用前必须初始化

//初始化:(通常在类构造函数中初始化)
	InitializeCriticalSection(&my_winsec);//初始化临界区

//临界区使用:
	EnterCriticalSection(&my_winsec);//进入临界区(加锁)
	myQueue.push_back(i);
	LeaveCriticalSection(&my_winsec);//离开临界区(解锁)

If there are mistakes or deficiencies, welcome to comment and point out! Creation is not easy, please indicate the source for reprinting. If it is helpful, remember to like and follow (⊙o⊙)
For more content, please follow my personal blog: https://blog.csdn.net/qq_43148810

Guess you like

Origin blog.csdn.net/qq_43148810/article/details/130649461