Reputation: 42822
I'm wondering if it is possible to implement the following logical under Linux and using POSIX thread library.
given a mutex
if (I can get the mutex) {
lock the mutex
call fun A
unlcok the mutex
}
else {
call fun B
}
I'm new to thread programming under Linux, so just use the pseudo-code to show the logic of the code snippet I'm looking for.
Upvotes: 1
Views: 253
Reputation: 4142
With the typical mutex lock, the thread will sleep until it gets the mutex, and when it does, it will lock all other threads out until it unlocks the mutex. pthread_mutex_trylock is what you need.
if(pthread_mutex_trylock())
{
mutex_unlock()
}
else
{
}
syntax is not correct...not sure what the return value of trylock is so look them up on manpage or google.
Upvotes: 1