"2017-06-20" Linux application Development engineer, one of the questions of the interview: Synchronization problem of Linux multi-thread

Source: Internet
Author: User
Tags mutex semaphore

Reference one: Three ways to synchronize Linux threads

Link Address: http://www.cnblogs.com/eleclsc/p/5838790.html

Brief answer:

The three most common methods of Linux thread synchronization are mutexes, conditional variables, and semaphores.

mutexes enable synchronization between threads through locking mechanisms, which are code that allows only one thread to execute a critical part at a time.

The condition variable is used to wait instead of locked, and the bar is used to automatically block a thread until a particular situation occurs, usually with the condition variable and the mutex used simultaneously.

The signal volume of a thread is the same as the concept of the semaphore used in interprocess communication, it is a special variable that can be increased or decreased, but the key access to it is guaranteed to be atomic, and if multiple threads in a program attempt to change the value of a semaphore, the system will ensure that all operations will proceed sequentially.

The most commonly used signal is a binary signal, and can only have 0 and 12 values, sometimes called single-value signal.

But there is one problem, how do you choose a semaphore or mutex when a thread synchronizes?

Here Baidu to one about mutual exclusion lock and semaphore difference:

1. The mutex is used for mutual exclusion of threads, and the semaphore is used for thread synchronization;

This is the fundamental difference between the two, that is, the difference between mutual exclusion and synchronization

Mutex: Refers to a resource that allows only one visitor to access it, uniqueness and exclusion, but mutual exclusion cannot restrict the order in which the visitor accesses the resource, that is, the access is unordered.  

Synchronization: It means that on the basis of mutual exclusion, this is the majority of cases, through other mechanisms to achieve the visitor's orderly access to resources.

In most cases, synchronization has been mutually exclusive, and in particular all write resources must be mutually exclusive, and in a few cases, multiple visitors can be allowed to access resources at the same time.

2. The mutex value can only be 0/1, and the semaphore value may be a non-negative integer.

In other words, a mutex can only be used for mutually exclusive access to a resource, and it cannot implement multi-threaded mutex issues for multiple resources.

The semaphore can realize multi-thread mutual exclusion and synchronization of multiple homogeneous resources.

When the semaphore is a single-valued semaphore, it is also possible to complete mutually exclusive access to a resource.

3. The lock and unlock of the mutex must be used by the same thread, the semaphore can be freed by one of the threads, and the other thread will get it.

"2017-06-20" Linux application Development engineer, one of the questions of the interview: Synchronization problem of Linux multi-thread

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.