The introduction

This article is the eleventh article, the mutex of thread synchronization, in the front of the “computer operating system foundation (four)– process management process synchronization” mentioned in the process synchronization and thread synchronization method, this is one of the thread synchronization methods – mutex. It is recommended to review the “computer operating system foundation (four)- process management process synchronization” this article, convenient to understand the following several on thread synchronization and process synchronization knowledge

The mutex

In process synchronization of the article has introduced to producers and consumers in the model, this model has two threads, act as the role of producers and consumers, respectively, in the case of concurrent, both threads are likely to operate at the same time the critical resource, if to operate at the same time the critical resources could thread synchronization problem, the mutex is one way to get rid of the thread synchronization

How do mutexes solve this problem?

Mutex is a guarantee that when one thread, such as thread 1, is manipulating a critical resource, it will prevent other threads from accessing it. This is how mutex works


In the previous producer-consumer model, the root cause of thread synchronization is that instructions from two threads are executed in a cross way, and the mutex ensures that instructions from two threads are not executed in a cross way


The effect of a mutex is also called atomicity, and the mutex guarantees the atomicity of these key instructions. Atomicity is:

  • Atomicity refers to the uninterruptible nature of a series of operations
  • The sequence of operations is either complete or not
  • There is no partial execution and partial non-execution

Just like the producer operation, the producer operation is divided into three instructions, depending on the atomic properties, these three instructions are either all completed or all not executed, there is no such thing as executing one or two of them and the CPU is robbed

  • Mutex is the simplest way to synchronize threads
  • Mutex, a variable in one of two states: unlock and lock
  • Two states can guarantee access to resources of serial (if a resource is locked, namely the resource used by one thread, another thread if you want to use the resources, can only wait for is use this thread to release resources, another thread can use the resources, thus ensure the resource access serial)

Code examples of mutex

Mutex is not used

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
$include<vector>
 // Critical resourcesint num=0;  / / producervoid *producer(void*){  int times= 100000000; // Loop a million times while(times--){ num += 1; // One product at a time } }  / / consumervoid *comsumer(void*){  int times = 100000000;  while(times--){ num -= 1; // Consume one product at a time } }  int main(a){  printf("Start in main function."); // Define two threads pthread_t thread1,thread2; // One executive producer logic and one executive consumer logic pthread_create(&thread1, NULL, &producer, NULL);  pthread_create(&thread2, NULL, &comsumer, NULL);  pthread_join(&thread1, NULL);  pthread_join(&thread2, NULL); // Prints the value of the critical resource printf("Print in main function: num = %d\n", num); } Copy the code

Running results:


Even though the number of producer and consumer cycles is the same, num does not run as zero, which creates a producer and consumer problem. Solve this problem with mutex

Use mutex

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
$include<vector>
 // Initialize the mutexpthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;  // Critical resourcesint num=0;  / / producervoid *producer(void*){  int times= 100000000; // Loop a million times while(times--){ / / lock pthread_mutex_lock(&mutex); num += 1; // One product at a time/ / unlock pthread_mutex_unlock(&mutex);  } }  / / consumervoid *comsumer(void*){  int times = 100000000;  while(times--){ / / lock pthread_mutex_lock(&mutex); num -= 1; // Consume one product at a time/ / unlock pthread_mutex_unlock(&mutex);  } }  int main(a){  printf("Start in main function."); // Define two threads pthread_t thread1,thread2; // One executive producer logic and one executive consumer logic pthread_create(&thread1, NULL, &producer, NULL);  pthread_create(&thread2, NULL, &comsumer, NULL);  pthread_join(&thread1, NULL);  pthread_join(&thread2, NULL); // Prints the value of the critical resource printf("Print in main function: num = %d\n", num); } Copy the code

Running results:


The result is 0, which means adding a mutex works. Locking actually takes longer to execute because of the performance penalty associated with locking

This is the content of the mutex, the sample is to use C language to write, all kinds of language are also provide the mutex API, PHP the mutex API can see here: https://www.php.net/mutex

It is the core competitiveness of a technician to seek invariance in the rapidly changing technology. Unity of knowledge and practice, theory and practice