Re: How to wait for multiple threads simultaneously?



On Feb 13, 3:52 am, Rainer Weikusat <rweiku...@xxxxxxxxxxx> wrote:

A 'spin lock' is one which does not involve blocking, but
busy-waiting.

Right, but this is not because of the general meaning of "spin" but
because a "spin lock" is special.

'Condition variables' are something threads can block
on, hence, they don't "spin" on them.

If they loop until something happens, they are spinning.

That blocking on pthread cond
vars may result in spurious wakeups is an accidental property of them.
The normal use is till to block on the condition variable until it is
signaled.

That's not the only reason you spin. Another thread have consumed the
predicate. Even if condition variables guaranteed no spurious wakeups,
many applications would still need to spin on th predicate.

To "spin" is simply to loop tightly, that is, without running complex
functional code in each iteration. The purpose of the loop is simply
to exit at the right time.

The verb itself describes a continous, fast activity. IMO, it's just
potentially confusing to use it for something completely different
(occasional retesting of a condition).

And yet using it the way I am using it seems to be common. For example
POSIX says:

"The pthread_spin_lock() function shall lock the spin lock referenced
by lock. The calling thread shall acquire the lock if it is not held
by another thread. Otherwise, the thread shall spin (that is, shall
not return from the pthread_spin_lock() call) until the lock becomes
available."

So all POSIX means by "spin" is "not return".

DS
.



Relevant Pages

  • Re: Locking etc. (Long, boring, redundant, newbie questions)
    ... What is really meant is that depending on the type of mutex a thread is trying to acquire, the thread will either spin or it will sleep waiting for the lock to become available. ...
    (freebsd-hackers)
  • Re: Lockless 63-bit Counter
    ... It would be much easier if the spin ... >> Obviously the xadds need the lock prefix. ... For memory where there is no ...
    (comp.lang.asm.x86)
  • Re: Spin lock + mutex in pthreads.
    ... When you give it a non-zero spin count, when you try to acquire the ... EnterCriticalSection first spins a few times ... Is there an equivalent construct in pthreads? ... "when a thread attempts to acquire a lock, and the lock is being held, the kernel examines the state of the thread that holds the lock. ...
    (comp.programming.threads)
  • Locking etc. (Long, boring, redundant, newbie questions)
    ... sleep waiting for the lock to become available. ... mutex protects. ... if we spin for so long ...
    (freebsd-hackers)
  • Re: [PATCH][5/8] Arch agnostic completely out of line locks / ppc64
    ... If another CPU holds it for a long time, ... * and that CPU is not asked to reschedule then *this* CPU will spin on the ... * lock for a long time, even if *this* CPU is asked to reschedule. ...
    (Linux-Kernel)