Say we have a shared resource that a bunch of different global queues have access to and, for the sake of this question, we use a Dispatch Semaphore to manage that access. When one of these global queues tells the semaphore to wait, the semaphore count is decremented and that thread has access to the shared resource. Is it possible that while the semaphore is waiting, another (different) global queue tries to access this shared resource, and the thread that GCD grabbed from its pool is the same thread that was grabbed for the previous queue (the queue that is currently making the semaphore wait) which would deadlock this thread and prevent the semaphore count from ever re-incrementing?
Can a Dispatch Semaphore inadvertently deadlock itself?
1k Views Asked by lurning too koad At
1
There are 1 best solutions below
Related Questions in IOS
- new thread blocks main thread
- Extracting viewCount & SubscriberCount from YouTube API V3 for a given channel, where channelID does not equal userID
- Display images on Django Template Site
- Difference between list() and dict() with generators
- How can I serialize a numpy array while preserving matrix dimensions?
- Protractor did not run properly when using browser.wait, msg: "Wait timed out after XXXms"
- Why is my program adding int as string (4+7 = 47)?
- store numpy array in mysql
- how to omit the less frequent words from a dictionary in python?
- Update a text file with ( new words+ \n ) after the words is appended into a list
Related Questions in GRAND-CENTRAL-DISPATCH
- new thread blocks main thread
- Extracting viewCount & SubscriberCount from YouTube API V3 for a given channel, where channelID does not equal userID
- Display images on Django Template Site
- Difference between list() and dict() with generators
- How can I serialize a numpy array while preserving matrix dimensions?
- Protractor did not run properly when using browser.wait, msg: "Wait timed out after XXXms"
- Why is my program adding int as string (4+7 = 47)?
- store numpy array in mysql
- how to omit the less frequent words from a dictionary in python?
- Update a text file with ( new words+ \n ) after the words is appended into a list
Related Questions in DISPATCHSEMAPHORE
- new thread blocks main thread
- Extracting viewCount & SubscriberCount from YouTube API V3 for a given channel, where channelID does not equal userID
- Display images on Django Template Site
- Difference between list() and dict() with generators
- How can I serialize a numpy array while preserving matrix dimensions?
- Protractor did not run properly when using browser.wait, msg: "Wait timed out after XXXms"
- Why is my program adding int as string (4+7 = 47)?
- store numpy array in mysql
- how to omit the less frequent words from a dictionary in python?
- Update a text file with ( new words+ \n ) after the words is appended into a list
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Short answer:
Yes, using semaphores can result in deadlocks, but not for the reason you suggest.
Long answer:
If you have some dispatched task waiting for a semaphore, that worker thread is blocked until the signal is received and it resumes execution and subsequently returns. As such, you don’t have to worry about another dispatched task trying to use the same thread, because that thread is temporarily removed from the thread pool. You never have to worry about two dispatched tasks trying to use the same thread at the same time. That is not the deadlock risk.
That having been said, we have to be sensitive to the fact that the number of worker threads in the thread pool is extremely limited (currently 64 per QoS). If you exhaust the available worker threads, then anything else dispatched to GCD (with the same QoS) cannot run until some of those previously blocked worker threads are made available again.
Consider:
That works fine. You have ten worker threads tied up with those
wait
calls and then the additional ten dispatched blocks callsignal
, and you’re fine.But, if you increase
count
to 100 (a condition referred to as “thread explosion”), the above code will never resolve itself because thesignal
calls are waiting for worker threads that are tied up with all of thosewait
calls. None of those dispatched tasks withsignal
calls will ever get a chance to run. And, when you exhaust the worker threads, that is generally a catastrophic problem because anything trying to use GCD (for that same QoS) will not be able to run.By the way, the use of semaphores in the thread explosion scenario is just one particular way to cause a deadlock. But for the sake of completeness, it’s worth noting that there are lots of ways to deadlock with semaphores. The most common example is where a semaphore (or dispatch group or whatever) is used to wait for some asynchronous process, e.g.
That can deadlock if (a) you run that from the main queue; but (b) the asynchronous method happens to call its completion handler on the main queue, too. This is the prototypical semaphore deadlock.
I only used the thread-explosion example above because the deadlock is not entirely obvious. But clearly there are lots of ways to cause deadlocks with semaphores.