Putting a thread to sleep using schedule() is foolproof?

Valdis.Kletnieks at vt.edu Valdis.Kletnieks at vt.edu
Wed Jun 11 13:51:55 EDT 2014


On Wed, 11 Jun 2014 23:09:05 +0530, Joshi said:

> set_current_state (TASK_INTERRUPTIBLE);
> schedule ();
>
> Is this a sure-shot way of putting a thread to sleep, or are there
> conditions when this may not put the calling thread into sleep?

This will only succeed in guaranteeing the thread sleep if the thread has done
something *else* to render it not schedulable.  schedule() will
return right back to that thread if it's the highest-priority thing
that's runnable.

What problem are you trying to solve?  Usually, you do that sort of
schedule() when you're doing something that will take a relatively long
chunk of time, and want other things to have a *chance* of running.  But
usually, you're perfectly happy with continuing to run if nobody else
wants to run.

Why did you want a guaranteed sleep?  If it's because you've started an
I/O and you *know* it will be 125 milliseconds before you can make further
progress, there's mdelay() and similar APIs... and so on for other reasons
for wanting to sleep (for instance, blocking on a lock has an API, etc)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140611/b113b99a/attachment.bin 


More information about the Kernelnewbies mailing list