Talk C chestnuts together (112nd back: C language instance-thread synchronization overview)
Hello, everyone. We talked about the example of inter-thread communication in the last time. This example is as follows:Thread Synchronization. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
When talking about synchronization, I think everyone is familiar with it, because we have introduced the content of Process Synchronization in the previous chapter. As we all know, a thread is a lightweight process. Although a thread does not need a dedicated communication mechanism like a process, a thread needs a dedicated synchronization mechanism. Today we will introduce the content of thread synchronization.
The meaning of synchronization is the same for processes and threads, but the methods of synchronization between processes and threads are different. We will not detail the meaning of synchronization. If you forget it, you can view the content in the previous chapter.
UsuallyThere are two ways to achieve Thread Synchronization:
Semaphores mutex
SemaphoresThe mechanism was proposed by computer scientist dijela. We have introduced it before, so we will not detail it. The core operation to implement synchronization is the P/V atomic operation on the semaphore. This mechanism is the same as the semaphore we mentioned during process synchronization in the previous chapter. If you forget it, you can view the content in the previous chapter. We are talking about the same means that the synchronization theory can be used in both processes and threads, but the specific implementation methods are different. We will introduce it in detail in the following chapter.
MutexThe mechanism means that a thread executes the lock operation before accessing a critical area. After the thread completes the operation on the critical area, it then performs the unlock operation. The locked critical area can only be used by the thread that locks it. It can only be accessed by other threads after the critical area is unlocked. The mutex mechanism is a mechanism dedicated to thread synchronization, which is not used in process synchronization. We will introduce this mechanism in detail in the following chapter.
Let's talk about the thread synchronization overview example. I want to know what examples will be provided later, and I will try again.