In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the difference between semaphores and mutexes in windows". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Differences: 1, mutex for thread mutex, semaphore for thread synchronization; 2, mutex value can only be 0 or 1, signal value can be non-negative integer; 3, mutex locking and unlocking must be used by the same thread, semaphore can be released by one thread, another thread gets.
The operating environment of this tutorial: windows7 system, Dell G3 computer.
The difference between mutexes and semaphores
1. Mutexes are used for thread mutexes and semaphores are used for thread synchronization.
This is the fundamental difference between mutexes and semaphores, that is, the difference between mutexes and synchronization.
Mutual exclusion: means that only one visitor is allowed to access a resource at the same time, which is unique and exclusive. However, mutual exclusion cannot limit the order in which visitors access resources, that is, access is unordered.
Synchronization: refers to the orderly access of visitors to resources through other mechanisms on the basis of mutual exclusion (in most cases). In most cases, synchronization is mutually exclusive, especially when all writes to resources must be mutually exclusive. In a few cases, multiple visitors can be allowed to access resources at the same time
two。 The mutex value can only be 0 stroke 1, and the signal value can be a non-negative integer.
In other words, a mutex can only be used for the exclusive access of one resource, and it can not realize the multi-thread mutex of multiple resources. Semaphores can realize multi-thread mutual exclusion and synchronization of multiple similar resources. When the semaphore is a single-valued semaphore, you can also complete the mutually exclusive access to a resource.
3. The locking and unlocking of mutexes must be used by the same thread respectively, and the semaphore can be released by one thread and obtained by another thread.
Mutex (Mutex)
Mutexes represent the data structure of mutual exclusion and are also used as binary signal lights. A mutex is basically a multitasking sensitive binary signal that can be used for synchronous multitasking behavior. It is often used to protect critical segments of code from interrupts and to share synchronized resources.
Mutex is essentially a lock that provides exclusive access to resources, so the main role of Mutex is for mutual exclusion. The value of the Mutex object, with only 0 and 1 values. These two values also represent the two states of Mutex. A value of 0 indicates the locked state, the current object is locked, and the user process / thread enters the queue if it tries to Lock the critical resource; a value of 1 indicates the idle state, the current object is idle, and the user process / thread can Lock the critical resource, and then the Mutex value minus 1 becomes 0.
Mutex can be abstracted into four operations:
-create a Create
-Lock Lock
-unlock Unlock
-destroy Destroy
Mutex can have an initial value when it is created, indicating whether the Mutex is locked or idle after it is created. In order to prevent deadlocks in the same thread, the system does not allow Mutex to be locked twice in a row (the system usually returns immediately on the second call). In other words, the corresponding operations of locking and unlocking need to be done in the same thread.
Mutex functions available in different operating systems:
Action / system
Win32
Linyx
Solaris
Create
CreateMutex
Pthread_mutex_init
Mutex_init
Add lock
WaitForSingleObject
Pthread_mutex_lock
Mutex_lock
Unlock
ReleaseMutex
Pthread_mutex_unlock
Mutex_unlock
Destroy
CloseHandle
Pthread_mutex_destroy
Mutex_destroy
Deadlocks occur mainly when there are multiple dependent locks, when one thread tries to lock mutexes in reverse order with another thread. How to avoid deadlocks is something that should be paid special attention to when using mutexes.
Generally speaking, there are several unwritten basic principles:
Be sure to acquire a lock before operating on a shared resource.
Be sure to release the lock after completing the operation.
Occupy the lock for as short a time as possible.
If there are multiple locks, such as the acquisition order is ABC chain buckle, the release order should also be ABC.
The lock it acquired should be released when the thread returns an error.
Some readers may wonder how the operations of "suspend wait" and "wake up waiting thread" are implemented. Each Mutex has a waiting queue, and a thread suspends waiting on the Mutex, first adds itself to the waiting queue, then puts the thread state to sleep, and then calls the scheduler function to switch to another thread. To wake up other threads in the waiting queue, a thread simply takes an item from the waiting queue, changes its state from sleep to ready, and joins the ready queue, so the next time the scheduler function executes, it is possible to switch to the awakened thread.
In general, if the same thread calls lock twice, on the second call, because the lock is already occupied, the thread will hang and wait for another thread to release the lock, but the lock is occupied by itself, and the thread is suspended without a chance to release the lock, so it will always be in a pending state, which is called Deadlock. Another typical deadlock situation is this: thread An acquires lock 1, thread B acquires lock 2, when thread A calls lock to try to acquire lock 2, the result is that thread B needs to suspend waiting thread B to release lock 2, and thread B also calls lock to try to obtain lock 1, the result is that thread A needs to suspend waiting thread A to release lock 1, so threads An and B are suspended forever. It is not difficult to imagine that if more threads and more locks are involved, the question of whether it is possible to deadlock will become complicated and difficult to judge.
Semaphore
Semaphores (Semaphore), sometimes called semaphores, are a facility used in a multithreaded environment that coordinates threads to ensure that they can use common resources correctly and reasonably.
Semaphores can be divided into several categories:
Binary semaphores (binary semaphore): only semaphores are allowed to take 0 or 1 values, which can only be obtained by one thread at the same time.
Integer semaphore (integer semaphore): the semaphore takes an integer value and can be obtained by multiple threads at the same time until the value of the semaphore changes to 0.
Recording semaphore (record semaphore): in addition to an integer value value (count), each semaphore has a waiting queue List, which is the identity of each thread blocking in the semaphore. When a semaphore is released and the value is added, the system automatically wakes a waiting thread from the waiting queue to get the semaphore, while the semaphore is reduced by one.
The semaphore controls access to the shared resource through a counter, and the value of the semaphore is a non-negative integer, which is reduced by one by all threads passing through it. If the counter is greater than 0, access is allowed and the counter is minus 1; if it is 0, access is disabled and all threads trying to pass through it will be in a waiting state.
The result of the counter calculation is a pass that allows access to the shared resource. Therefore, in order to access shared resources, the thread must get a pass from the semaphore, and if the count of the semaphore is greater than 0, the thread gets a pass, which will cause the count of the semaphore to decrease, otherwise, the thread will block until it gets a pass. When this thread no longer needs to access a shared resource, it releases the pass, which causes the semaphore count to be incremented, and if another thread waits for the pass, that thread will get the pass at that time.
Semaphore can be abstracted into five operations:
-create a Create
-wait for Wait:
The thread waits for the semaphore, and if the value is greater than 0, the value is minus one; if it is only equal to 0, the thread goes to sleep until the semaphore value is greater than 0 or times out.
-release Post
If the release semaphore is executed, the value is incremented by one; if there is a waiting thread at this time, the thread is awakened.
-trying to wait for TryWait
If TryWait is called, the thread does not really get the semaphore, or checks to see if the semaphore can be obtained. If the semaphore value is greater than 0, TryWait returns success; otherwise, it returns failure.
-destroy Destroy
Semaphores can be used to protect two or more key code segments that cannot be called concurrently. Before entering a critical code segment, the thread must obtain a semaphore. If there are no threads in the critical code snippet, the thread immediately enters that part of the block diagram. Once the critical code snippet is complete, the thread must release the semaphore. Other threads that want to enter this critical segment of code must wait until the first thread releases the semaphore. To complete this process, you need to create a semaphore and place Acquire Semaphore VI and Release Semaphore VI at the beginning and end of each key code snippet. Verify that these semaphores VI refer to the semaphores that were originally created.
Action / system
Win32
POSIX
Create
CreateSemaphore
Sem_init
wait for
WaitForSingleObject
Sem _ wait
Release
ReleaseMutex
Sem _ post
Trying to wait.
WaitForSingleObject
Sem _ trywait
Destroy
CloseHandle
Sem_destroy
This is the end of the content of "what is the difference between semaphores and mutexes in windows". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.