Talk C chestnuts together (112nd back: C language instance-thread synchronization overview)

Source: Internet
Author: User
Tags overview example

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.