Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming.threads > #3436
| Path | csiph.com!aioe.org!.POSTED!not-for-mail |
|---|---|
| From | Kaz Kylheku <221-501-9011@kylheku.com> |
| Newsgroups | comp.programming.threads |
| Subject | Re: Is mutex ownership handed off strictly only to threads which have requested for the lock prior to unlocking? |
| Date | Wed, 31 Aug 2016 17:59:42 +0000 (UTC) |
| Organization | Aioe.org NNTP Server |
| Lines | 53 |
| Message-ID | <20160831104743.790@kylheku.com> (permalink) |
| References | <e977edab-75c7-4d0c-92f2-5049cc37383f@googlegroups.com> |
| NNTP-Posting-Host | OGJi3KNpFOhM58UHZwXj0w.user.gioia.aioe.org |
| X-Complaints-To | abuse@aioe.org |
| User-Agent | slrn/pre1.0.0-18 (Linux) |
| X-Notice | Filtered by postfilter v. 0.8.2 |
| Xref | csiph.com comp.programming.threads:3436 |
Show key headers only | View raw
On 2016-08-31, Sibin Thomas <sibinpthomas@gmail.com> wrote: > Situation: > > 1. Thread 1 currently owns the mutex. > 2. While Thread 1 retains ownership of the mutex, Thread 2 makes a request for the same lock. > 3. Thread 1 unlocks the lock. > > At this juncture can another thread (say, Thread 3) swoop in and make > a request for the lock and acquire it (as shown in the image below)? This may depend on the mutex attributes. Such a requirement is not defined for any of the standard mutex types (PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT, etc). A strict mutex like this could be provided as some platform-specific type. A pthread implementation could even map the default type to such a mutex. (Since that will negatively affect throughput for the sake of fairness, unlikely). > Or does POSIX guarantee that a mutex shall be acquired by a thread > that is already waiting for the said mutex at the moment of unlocking > a mutex? > > http://i.stack.imgur.com/yy937.png > > The man page for `pthread_mutex_unlock()` states that - > >> If there are threads blocked on the mutex object referenced by mutex >> when pthread_mutex_unlock() is called, resulting in the mutex becoming >> available, the scheduling policy shall determine which thread shall >> acquire the mutex. > > This seems to say that Thread 3 can not swoop in and acquire the mutex, though I am not fully assured. Yes, on a single processor, the scheduling policy will effectively decide that, taking into consideration thread priorities. The unlock operation will wake up a waiting thread. So then in your scenario, you have three threads which are runnable. The original thread (which is presumably going off to do something else and not contending for the mutex), the woken thread, and the opportunistic one. Only one thread at a time can run on the processor. Between the two threads which want the mutex, the one that will get it will be the one which runs first, and that is up to the scheduler. On a multiprocessor, the opportunistic thread can, however, be actually executing on one processor as the mutex is unlocked on another processor, and can perhaps snatch the mutex via an atomic operation not even involving a trip to the kernel. The thread which was woken from waiting on the mutex tries the same atomic operation and finds that it fails.
Back to comp.programming.threads | Previous | Next — Previous in thread | Find similar | Unroll thread
Is mutex ownership handed off strictly only to threads which have requested for the lock prior to unlocking? Sibin Thomas <sibinpthomas@gmail.com> - 2016-08-30 22:39 -0700
Re: Is mutex ownership handed off strictly only to threads which have requested for the lock prior to unlocking? Casper H.S. Dik <Casper.Dik@OrSPaMcle.COM> - 2016-08-31 08:24 +0000
Re: Is mutex ownership handed off strictly only to threads which have requested for the lock prior to unlocking? Sibin Thomas <sibinpthomas@gmail.com> - 2016-08-31 04:08 -0700
Re: Is mutex ownership handed off strictly only to threads which have requested for the lock prior to unlocking? Casper H.S. Dik <Casper.Dik@OrSPaMcle.COM> - 2016-08-31 11:41 +0000
Re: Is mutex ownership handed off strictly only to threads which have requested for the lock prior to unlocking? Sibin Thomas <sibinpthomas@gmail.com> - 2016-08-31 10:08 -0700
Re: Is mutex ownership handed off strictly only to threads which have requested for the lock prior to unlocking? Kaz Kylheku <221-501-9011@kylheku.com> - 2016-08-31 18:08 +0000
Re: Is mutex ownership handed off strictly only to threads which have requested for the lock prior to unlocking? Sibin Thomas <sibinpthomas@gmail.com> - 2016-09-01 03:50 -0700
Re: Is mutex ownership handed off strictly only to threads which have requested for the lock prior to unlocking? Kaz Kylheku <221-501-9011@kylheku.com> - 2016-08-31 17:59 +0000
csiph-web