Reputation:
I just wondered how would I get(count) the threads are held for locking a mutex, for example considering the following example
void _foo(void* arg){
pthread_mutex_lock(&_lock);//[ABC]
//doing something
pthread_mutex_unlock(&_lock);
}
so the question: how would I find out how many threads are currently waiting to get lock the _lock
at [ABC]
in pthread? or any other parallel lib.
and a little more confusing question, is it possible to redirect the waiting threads to somewhere else? for example just after the first thread process, waiting threads will be redirected to _bar()
method, I mean change the code execution at runtime.
Upvotes: 0
Views: 79
Reputation: 70911
1st question: Count them up, before letting them run into the lock. Count them down after having left the locked state.
2nd question: Look at pthread_mutex_trylock()
Depending on its result (EBUSY
or not) continue with foo()
or call bar()
.
Upvotes: 1
Reputation: 500267
I don't think there's a standard pthreads way to get the count of waiting threads.
As to the second question, you could use pthread_mutex_trylock()
.
Upvotes: 1