Sunday, September 27, 2009

Usage of ::Sleep(0)

The Sleep() function is normally used to put a thread to sleep for a particular time period. The time period is passed as the parameter to the Sleep() function. The statement Sleep(0) has even a hand on thread scheduler in case of multithreaded application. It can be explained with a small example that you have an application which fills 5 different colored balls uniformly in a bucket with 5 threads running at a time. Each thread is assigned with a color. If you just allow each thread to run in a loop, all the threads will try to grab the maximum CPU time it can get without bothering about the other threads running in parallel. This doesn't give a color uniformity for the filling process. you can maintain this uniformity by putting a Sleep(0) statement in each thread at the place where you have finished putting a single ball. This will relinquish the CPU for the next thread allowing it to put a ball into bucket.

No comments:

Post a Comment