Re: Spin lock + mutex in pthreads.
- From: Surinder Singh <Surinder.Singh@xxxxxxxxxxx>
- Date: Sun, 11 May 2008 20:50:09 +0530
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 **
.
- References:
- Spin lock + mutex in pthreads.
- From: jason.cipriani@xxxxxxxxx
- Spin lock + mutex in pthreads.
- Prev by Date: [Commercial] Advance C and Linux System Programming: Hands-On Training
- Next by Date: A site for all your programming language needs
- Previous by thread: Re: Spin lock + mutex in pthreads.
- Next by thread: general threads question
- Index(es):
Relevant Pages
|