Hello Everyone,
I observed this issue while running one of my programs.
I have a mutex and I start multiple threads ( all of same priority) which access this mutex. Does the linux scheduler guarantee that the threads will be invoked in the order in which they tried to access the mutex ? Or will it invoke any one of them from the READY queue ?
My observation is that it wakes up one of the threads randomly in the ready queue. Is this right or am I missing something?
My code more or less looks something like this.
FuncX ()
{
while(something)
{
pthread_create(FuncY);
}
}
FuncY( )
{
pthread_mutex_lock(mutex);
/* do something */
pthread_mutex_unlock(mutex);
}
If it indeed wakes up the threads in a random order, is there a way I can make the thread that first locked the mutex to wake up first once the mutex is released?
Re: Question on Linux Scheduler
Hi,
> if it indeed wakes up the threads in a random order, is there a way I can make
> the thread that first locked the mutex to wake up first once the mutex is
> released?
Try using SCHED_FIFO in pthread_setschedparam. this should force the threads to be executed only in the way they are created.
But I think you will need to be super user for that. Better still, go through the man (pthread_getschedparam, pthread_setschedparam), it would explain you much better.
And, what is happening in your pogram is the expected behaviour because Linux kernel would not distinguish between first sleeping and last sleeping thread on a mutex. That is because all your threads would have same priority (I am assuming that, until you have exlicitly changed that) and would be treated like similar LWPs (as far as I can trust my knowledge on this :) ).
Regards
Shreyansh