Guide to the use of c language multi-thread synchronization under Linux

One, multi-threaded
header file:

#include<pthread.h>

Function declaration:

 int pthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict_attr,void**start_rtn)(void*),void *restrict arg);

The parameters are in order:
  pointer to thread identifier, setting thread attributes, starting address of thread running function, and incoming parameters.

Edible method:
pointer function:

void *mythread_function(void *arg)
{
    
    
  ...
}

Calling code:

...
#include <pthread.h>
...
pthread_t mythread;
pthread_create(&mythread, NULL, mythread_function, NULL)

note:

The fourth parameter of pthread_create is to pass a parameter to the thread, but because it can only pass in one, it needs to be encapsulated with struct when there are many parameters.
Thread creation successfully returns 0.2
. Semaphore
Header file:

  #include <semaphore.h>

function:

Initialize the semaphore

  int sem_init(sem_t *sem, int pshared, unsigned int val);

The parameters are:
  semaphore pointer, semaphore type, and semaphore initial value.
  When the second parameter pshared is 0, all threads in the process are available, and when it is not 0, it is available between different processes.
Semaphore minus 1

 int sem_wait(sem_t *sem);

Explanation:
  This function applies for a semaphore, and waits if there is no available semaphore. When there is an available semaphore, it occupies a semaphore and subtracts 1 from the value of the semaphore.
Semaphore plus 1

   int sem_post(sem_t *sem);

Destroy semaphore

 int sem_destory(sem_t *sem);

This function destroys the semaphore.
3. Mutex lock The
header file and thread are the same:

 #include <pthread.h>

Instructions:

Create
  Method 1:

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

Method 2:
  pthread_mutex_init function, function prototype:

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t * attr)

Note: The second parameter is to set the mutex lock attribute, NULL means the default is a normal lock.
Property
  Setting method:
   Method 1:

pthread_mutexattr_init(pthread_mutexattr_t *mattr);

Way two:

pthread_mutexattr_setpshared(pthread_mutexattr_t *mattr, int pshared);

Way three:

pthread_mutexattr_settype(pthread_mutexattr_t *attr , int type)

Note: The second parameter is the scope of setting the mutex: you can specify whether the process is synchronized with other processes or between different threads in the same process. Can be set to PTHREAD_PROCESS_SHARE and PTHREAD_PROCESS_PRIVATE. The latter is the default, which means the lock is used in the process.
Acquiring lock type:

 pthread_mutexattr_gettype(pthread_mutexattr_t *attr , int *type)

The type of mutex lock:
1. PTHREAD_MUTEX_TIMED_NP, which is the default value, which is a normal lock. When a thread locks, the remaining threads requesting the lock will form a waiting queue and obtain the lock according to priority after unlocking. This lock strategy ensures the fairness of resource allocation.
2. 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 compete again when the locking thread is unlocked.
3. PTHREAD_MUTEX_ERRORCHECK_NP, error detection lock, if the same thread requests the same lock, it returns EDEADLK, otherwise the action is the same as PTHREAD_MUTEX_TIMED_NP. This ensures that the simplest case of deadlock will not occur when multiple locks are not allowed.
4. PTHREAD_MUTEX_ADAPTIVE_NP, adaptive lock, the simplest type of lock, only waiting to be unlocked and compete again.

Lock destruction

int pthread_mutex_destroy(pthread_mutex_t *mutex);

Lock

int pthread_mutex_lock(pthread_mutex_t *mutex);

Unlock

int pthread_mutex_unlock(pthread_mutex_t *mutex);

Try to lock (return to EBUSY when occupied instead of suspend waiting)

int pthread_mutex_trylock(pthread_mutex_t *mutex);

Need C/C++ Linux server architect learning materials plus group (812855908) to obtain (data including C/C++, Linux, golang technology, Nginx, ZeroMQ, MySQL, Redis, fastdfs, MongoDB, ZK, streaming media, CDN, P2P, K8S , Docker, TCP/IP, coroutine, DPDK, ffmpeg, etc.)
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40989769/article/details/109293200