Thread safety---mutex locks and read-write locks

mutex lock

    If the value of the semaphore is at most 1 , it is actually equivalent to a shared resource having at most one line at any time.
process is accessing, this logic is called "mutual exclusion". At this time, there is a more convenient and semantically accurate tool to satisfy
Based on this logic, it is a mutex lock.
      "Lock" is a very vivid way of saying it: just like a room can only accommodate one person, anyone who enters will lock it.
The door is locked, and no one else can enter until the person who enters opens the lock again, which releases the lock resource.
end.
    The operations on mutex locks are nothing more than: initialization, locking, unlocking, and destruction. The code below shows two threads by
How to use a mutex lock to provide mutually exclusive access to standard output.
#include<stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

char *mem_map=NULL;

pthread_mutex_t lock;

void *function(void *arg)
{
    char *msg=(char *)arg; 
    printf("线程号:%ld 收到参数:%s\n",pthread_self(),msg);
   while (1)
  {
       printf("\nfunction等待锁 \n");
    pthread_mutex_lock(&lock);
    printf("function获取资源 已上锁\n ");
     printf("msg:%s\n",mem_map);
      sleep(1);
    pthread_mutex_unlock(&lock);
   
  }
}
int main(int argc, char const *argv[])
{
    pthread_mutex_init(&lock,NULL);
   mem_map=calloc(1,32);
   pthread_t thread;
   pthread_create(&thread, NULL,function, "Hello Gz123");   
  while (1)
 {
    printf("main 等待资源....\n ");
    pthread_mutex_lock(&lock);
    printf("main获取资源 以上锁 \n");
     printf("main获取成功发送数据:");
   fgets(mem_map,32,stdin);
    sleep(1);
   printf("main 释放资源\n");
   pthread_mutex_unlock(&lock);
 }
   pthread_join(thread,NULL);
    return 0;
}

read-write lock

       The inefficiency of mutex locks is that there is no more detailed distinction between how to access shared resources, and one-size-fits-all access is available at any time.
Only one thread is allowed to access shared resources, but the fact is that read operations can be performed at the same time, and only write operations require each other.
Exclusion, so if you can add read locks (can be added repeatedly) or write locks (only
Allowing one at a time) can greatly improve efficiency (especially when there are a large number of read operations).
      The operation of read-write lock is almost the same as that of mutex lock. The only difference is that you can choose to add read or write when locking.
Lock, the following code shows how to use it:
#include<stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
//定义线程间共享资源

//定义一个指针作用与线程之间的共享内存
char *mem_map=NULL;

pthread_rwlock_t rwlock;

void *function(void *arg)
{
    char *msg=(char *)arg;
   
   while (1)
{
    pthread_rwlock_rdlock(&rwlock);
     printf("线程%s号:%s \n",msg,mem_map);
    pthread_rwlock_unlock(&rwlock);
    sleep(2);
   
}
}
int main(int argc, char const *argv[])
{
  
    pthread_rwlock_init(&rwlock,NULL);

    mem_map=calloc(1,32);

      pthread_t thread1;
      pthread_t thread2;
      pthread_t thread3;
      pthread_t thread4;
     pthread_create(&thread1, NULL,function, "1");   
     pthread_create(&thread2, NULL,function, "2");   
     pthread_create(&thread3, NULL,function, "3"); 
     pthread_create(&thread4, NULL,function, "4");   
while (1)
{
    printf("main 等待上写锁....\n ");
    pthread_rwlock_wrlock(&rwlock);
    printf("main获取资源成功上写锁 \n");
    fgets(mem_map,128,stdin);
    pthread_rwlock_unlock(&rwlock);
    printf("解除写锁....\n");
    sleep(5);
}
   pthread_join(thread1,NULL);
   pthread_join(thread2,NULL);
   pthread_join(thread3,NULL);
   pthread_join(thread4,NULL);
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_52467164/article/details/127563070