Re: Stuck notes in Rax and Synthtest
Re: Stuck notes in Rax and Synthtest
- Subject: Re: Stuck notes in Rax and Synthtest
- From: Art Gillespie <email@hidden>
- Date: Wed, 7 May 2003 13:18:18 -0400
Could anyone briefly explain a newbie what a mutex is and how it's
used? (A link would do)
Mutex = Mutual Exclusion
- Serializes access to a critical region of code or data.
void importantShitThatNeedsToBeSerialized ()
{
/**
* Note this will block if the lock is already in use
* If you can't afford this behavior (e.g. in a realtime thread),
* use pthread_mutex_trylock and check the return value for EBUSY
* In either case NEVER call pthread_mutex_unlock unless
pthread_mutex_lock
* returned 0.
*/
pthread_mutex_lock(myMutex);
// do important shit here
pthread_mutex_unlock(myMutex);
}
(see man pthread_mutex_lock, man pthread_mutex_trylock, and man
pthread_mutex_unlock for more)
Create your mutex -
pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER;
- or with -
int pthread_mutex_init ( pthread_mutex_t *mutex, pthread_mutexattr_t
*attr);
(see man pthread_mutex_init for more)
Destroy your mutex -
int pthread_mutex_destroy (pthread_mutex_t *mutex);
(see man pthread_mutex_destroy for more)
Note that the case may arise where you need mutual exclusion that
*isn't* owned by the locking thread... in this case, you need to use a
semaphore instead. (Since semaphores aren't part of Pthreads, you'll
have to look into BSD's compliance with POSIX 1003.1b-1993).
Semaphores are cool for work queues/synchronization because you can
have a bunch of threads *park* on a semaphore and when there's
something ready for them to do, another thread can notify the semaphore
that either one or all of the waiting threads can continue.
Hope that helps.
Best,
Art
>>0xBA
Cheers,
;) Urs
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.