Re: Spin lock + mutex in pthreads.



jason.cipriani@xxxxxxxxx wrote:
The Windows API provides the function
InitializeCriticalSectionAndSpinCount:

http://msdn.microsoft.com/en-us/library/ms683476(VS.85).aspx

When you give it a non-zero spin count, when you try to acquire the
critical section later, EnterCriticalSection first spins a few times
before putting the thread to sleep.

Is there an equivalent construct in pthreads? I see pthreads has spin
locks (e.g. pthread_spin_lock), but I don't see anything that combines
these with pthread mutices.

Also, just to make sure: I have an application (that uses pthreads)
with two threads where a performance-critical thread has some data
that it frequently reads, and that data is written to by the second
thread very, very infrequently. The read and write operations are very
fast, but because the writes happen so infrequently I don't want the
overhead of locking/unlocking real mutices all the time in the first
thread. Are spin locks what I want to use here?

Thanks,
Jason

Jason,

This is mutex/pthread behavior on Solaris
<quote>
"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. If the lock holder (owner) is running on a processor, the thread attempting to get the lock will spin. If the thread holding the lock is not running, the thread attempting to get the lock will block </quote>
from http://sunsite.uakom.sk/sunworldonline/swol-09-1999/swol-09-insidesolaris.html

Also, check running pthread_mutex_lock() and then pthread_mutex_unlock() and see if it is really taking significant percentage of cpu compared to
your main task. As you said write is infrequent so overhead you have is
that of lock/unlock only and ignorable block time. After measuring cpu
share of lock/unlock, you can decide if they are to be avoided.

pthread_spin_lock(3C)/pthread_spin_unlock(3C) will also have overhead.

In either case two functions overhead is there. You can time them which is more per N number of calls.

In spin lock your reader or writer will not be context switched out just
because of lock. But with adaptive mutex in solaris, it will be nearly
same in your case as writer will lock mutex not so frequently.

Thanks
- Surinder
** Posted from http://www.teranews.com **
.



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: RFC: Ideal Adaptive Spinning Conditions
    ... I haven't seen your patches but you are not doing a heuristic approach, ... do not "spin" hoping the lock will suddenly become ... I'm talking about the original idea people had of "lets spin for 50us ... I'm still seeing performance penalties even on the shortest ...
    (Linux-Kernel)
  • 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)
  • Java-like synchronization for pthreads with C++.
    ... I'm developing a multi threaded C++ app under Linux using pthreads and ... In other words, while a thread holds a lock on a resource, the ... I have currently implemented a simple binary lock class using ipc ... semaphores that allows a single thread to grab and release a lock around ...
    (comp.unix.programmer)