Unix/Linux Process and thread-to-thread synchronization technical summary [learning summary, please do not vomit ...]

Source: Internet
Author: User

To allow data sharing between threads or processes, synchronization is usually required. Common Synchronization Methods include mutex lock, condition variable, read/write lock, and semaphore. In addition, synchronization between processes can also be performed through inter-process communication, including pipelines (unknown pipelines and famous pipelines), semaphores, message queues, shared memory, and remote process calls can also be controlled over the network through sockets.

I. mutex lock and condition variable are the basic components of synchronization.

Mutex locks and condition variables come from the posix.1 thread standards and are used by various threads in the same process in the same step. However, if the two are stored in the memory zone shared by multiple processes, they can also be used for inter-process synchronization.

1. mutex lock is used to protect the critical section, so that only one thread can executeCodeThe general outline is as follows:

Lock_the_mutex (...);

Critical Section

Unlock_the_mutex (...);

The following three functions lock and unlock a mutex lock:

# Include <pthread. h>

Int pthread_mutex_lock (pthread_mutex_t * mptr); // If the lock cannot be obtained immediately, it will be blocked here

Int pthread_mutex_trylock (pthread_mutex_t * mptr); // If the lock cannot be obtained immediately, ebusy is returned. You can perform other operations based on the returned value in non-blocking mode.

Int pthread_mutex_unlock (pthread_mutex_t * mptr); // release the lock

Mutex lock is usually used to protect the shared data shared by multiple threads or processes)

2. Condition variable, which is a sending and waiting signal. The user locks the mutex lock, and the condition variable is used for waiting. In general, calling pthread_cond_wait (...) in a process/thread (..) wait for a condition to be established. At this time, the process is blocked. Another process/thread performs some operation. When a condition is set, pthread_cond_signal (...) is called (...) so that pthread_cond_wait (...). It should be noted that the signal mentioned here is not a system-level sigxxxx signal, but it is easier to understand with the word "signal. The condition variable is closer to the semaphore or can be considered as a semaphore.

The following two functions are used to control conditional variables:

# Include <pthread. h>

Int pthread_cond_wait (pthread_cond_t * cptr, pthread_mutex_t * mptr );

Int pthread_cond_signal (pthread_cond_t * cptr );

From the code, we can see that the use of condition variables requires a lock mechanism, that is, the mutex lock mentioned above. That is to say, a process/thread must wait until the shared data in the critical section reaches a certain state before performing some operation, and this state is true, then, a signal is sent to notify another process/thread.

In fact, the pthread_cond_wait function can also use a while endless loop to wait for the condition to be set up. However, it should be noted that using the while endless loop will seriously consume the CPU, pthread_cond_wait adopts the thread sleep mode, which is a waiting mode rather than a constant check mode.

In general, the code for sending signals to conditional variables is roughly as follows:

Pthread_mutex_lock (& mutex );

Set the condition to true

Pthread_cond_signal (& Cond); // send a signal

Pthread_mutex_unlock (& mutex );

The code for waiting condition and going to sleep to wait for the condition to become true is roughly as follows:

Pthread_mutex_lock (& mutex );

While (the condition is false)

Pthread_cond_wait (& cond, & mutex );

Execute an operation

Pthread_mutex_unlock (& mutex );

Note that pthread_cond_wait (& cond, & mutex) is a yard operation. When it is executed, unlock mutex first, in this way, other threads can get the lock to modify the condition. After pthread_cond_wait is unlocked, the thread/process will be put into sleep. In addition, when the function returns, mutex will be locked again, in this way, the lock can be unlocked after an operation is executed.


Ii. read/write locks

As the name suggests, read/write locks are also a kind of lock. They are improved based on the mutex lock. When a process/thread obtains the write lock, other processes/Threads can still obtain the lock, only the read lock is obtained. Because a process/thread writes data, the read operations of other processes/threads are not affected.

 

3. semaphores

Semaphore is a primitive used to provide synchronization means between different processes or threads. You can understand it.

Process a process B

\/

Process \/

----------------------------------------------------

Kernel \/

Semaphores

That is to say, the semaphores are maintained by the kernel and are independent of processes. Therefore, you can use it for synchronization.

Generally, it is based on the famous POSIX semaphore and can be considered as a special file in the system (because everything in Linux can be considered as a file ), because many processes are used for communication and synchronization, if it is a synchronization between threads, it is often used based on POSIX memory semaphores. (The memory-based semaphores must be specified during creation to determine whether to share among processes. The famous semaphores must be manually deleted as the kernel persists, and the memory-based semaphores must be continuously deleted with the process)

The working principle of semaphores is similar to mutex lock + condition variable.

The main functions are sem_open, sem_close, and sem_unlink. Note that close only closes the semaphores but does not delete them from the system. Unlink deletes the semaphores.

The sem_wait and sem_trywait functions are similar to those of pthread_cond_wait. They both wait for the establishment of a condition. The difference between sem_wait and sem_trywait is that when the specified semaphore value is 0, the latter does not put the caller into sleep, but immediately returns eagain, that is, retry.

Sem_post and sem_getvalue functions. sem_post adds one of the specified semaphores, and then wakes up any thread waiting for the signal value to become a positive number. Sem_getvalue is a function used to obtain the current signal value.

 

Summary:

Differences among mutex lock, condition variable, and semaphore:

(1) The mutex lock must always be unlocked by the lock thread (because other threads cannot obtain the lock at this time). The semaphore does not have this restriction: A thread waits for a semaphore, another thread can mount the semaphore.

(2) Each semaphore has a value associated with it. When the output is + 1 and the wait time is-1, any thread can generate a signal, even if no thread is waiting for the semaphore value. However, for conditional variables, if no thread is blocked on pthread_cond_wait after pthread_cond_signal, the signal on this conditional variable is lost.

(3) ability to process signals from various synchronization TechniquesProgramThe only function that can be safely called in is sem_post.

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.