purpose of sleep(0)?
i''m new to HawkNL (www.hawksoft.com - a portable low-level networking library), but i''m learning it (for a mp game we''re making), and today finally made a server/client app, where server notifices server when he wants to disconnect. i did that myself (lol, i know it''s nothing, but still... :D).
but as i''ve looked around some code, mostly thread related code, i''ve noticed sleep(0); a lot. now, could someone please explain to me what that does? i''m pretty new to threads because i''ve never worked with them until now, so i''m reading the tut at pthread site, and learning it. but i didn''t find anything about sleep(0);. one of the comments for it was // yield to the OS, allow CPU to context switch. what does that mean?
thanks for help.
---
shurcool
my project
Well if I remember it makes it sit there until it recives a message of some kind like if some one clicks on the window you know. Thats what i think it does if my memory servers me correctly.
Context switch -> the OS switches which thread is currently running.
sleep() usually sends the thread inactive for the specified number of milliseconds, after which it is eligible to be chosen by the OS to run agian. sleep(0) will basically let the OS look around and see if there is any other higher priority threads that it should run first, but if not then it will carry on with minimal delay.
Threading is very OS specific as to how specific thread issues are handled, so results on different OS''s may vary.
_@/
sleep() usually sends the thread inactive for the specified number of milliseconds, after which it is eligible to be chosen by the OS to run agian. sleep(0) will basically let the OS look around and see if there is any other higher priority threads that it should run first, but if not then it will carry on with minimal delay.
Threading is very OS specific as to how specific thread issues are handled, so results on different OS''s may vary.
_@/
"yield to the OS, allow CPU to context switch" is exactly what that means : sleep() is an operating system call. You''re giving control to the OS for a brief moment (you''re not asking the program to ''sleep'' at all, since the parameter is 0). Then the OS may decide to give control to another thread instead (context switch).
This is to prevent ''tight'' loops, among other things. A thread may hog the CPU if it is in a very small loop. By putting a sleep(0), you''re giving other threads an opportunity to run.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
This is to prevent ''tight'' loops, among other things. A thread may hog the CPU if it is in a very small loop. By putting a sleep(0), you''re giving other threads an opportunity to run.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
Sleep(0) relinquishes the threads time-slot, but places it back into the ready-to run queue (as opposed to the thread-sleeping queue).
If another thread is ready to run, it gets focus. If no other threads are ready to run, no context switch is performed, and sleep(0) returns immediately.
It’s always good to place a sleep inside of any loop. If you want to do background processing with all the idle CPU time, you’d set your thread priority to 2 (idle) and call sleep(0) (as opposed to sleep(10) or something higher).
If another thread is ready to run, it gets focus. If no other threads are ready to run, no context switch is performed, and sleep(0) returns immediately.
It’s always good to place a sleep inside of any loop. If you want to do background processing with all the idle CPU time, you’d set your thread priority to 2 (idle) and call sleep(0) (as opposed to sleep(10) or something higher).
Sleep(0) tells the OS that the current thread does not require the remaining part of its time slice. So the OS performs a thread swichting when executing Sleep(<anything>. For an example:
while (!bTerminate)
{
Sleep(0);
}
If you dont need this function to response at once it also can be useful to use higher values with Sleep. When used with n msecs the CPU wont come back to your thread until the specified time has passed.
So Sleep(0) is some kind of "fair-play". Use what I cannot use now. (<> CPU Time)
while (!bTerminate)
{
Sleep(0);
}
If you dont need this function to response at once it also can be useful to use higher values with Sleep. When used with n msecs the CPU wont come back to your thread until the specified time has passed.
So Sleep(0) is some kind of "fair-play". Use what I cannot use now. (<> CPU Time)
wow guys, thanks a lot for such fast and easy to understand responces! i get it, if i knew this earlier i could''ve solved a problem i couldn''t a long time ago (i found a walk-around, but this would''ve been better)! great stuff. now i just gotta learn mutexes, condion vars, etc. and i''ll be ready to use threads.
i''m just wondering, how would i set the priority of a thread? i''m using pthreads, and i haven''t seen anything like that (not that i''ve looked at the whole thing that much either). :-\ i''ll look again now.
---
shurcool
my project
i''m just wondering, how would i set the priority of a thread? i''m using pthreads, and i haven''t seen anything like that (not that i''ve looked at the whole thing that much either). :-\ i''ll look again now.
---
shurcool
my project
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement