Google
 
   
Login
Username:

Password:


Lost Password?

Register now!
Search
Main Menu
service
top books
Polls
What do you think about php-deluxe.net?
Excellent!
Cool
Hmm..not bad
What the hell is this?
encyclopedia
recommendation
Freenet DSL
Who's Online
13 user(s) are online (13 user(s) are browsing encyclopedia)

Members: 0
Guests: 13

more...
partner

Busy waiting

In software engineering, busy waiting or spinning is a technique in which a process (computing) repeatedly checks to see if a condition is true, such as waiting for Computer keyboard input or waiting for a lock (computer science) to become available. It can also be used as a naïve way to simply delay execution for some amount of time. Spinning can be a valid strategy in certain special circumstances, most notably in the implementation of spinlocks within operating systems designed to run on Symmetric multiprocessing systems. In general, however, it should be avoided, as the Central processing unit time spent waiting could have been reassigned to another thread (computer science).

= Example C code =

The C code below shows two threads that share a global integer i. The first thread uses busy waiting to check for a change in the value of i.

#include #include #include int i; /* i is global, so it is visible to all functions. */ /* t1 uses spin lock to wait for i to change from 0. */ static void *f1() { while (i == 0) {} /* just keep checking over and over. */ printf( i s value has changed to %d. , i); return; } static void *f2() { sleep(60); /* sleep for 60 seconds. */ i = 99; printf( t2 changing the value of i to %d. , i); return; } int main() { int x; pthread_t t1, t2; i = 0; /* set global int i to 0. */ x = pthread_create(&t1, NULL, f1, NULL); if (x != 0) printf( pthread foo failed. ); x = pthread_create(&t2, NULL, f2, NULL); if (x != 0) printf( pthread bar failed. ); pthread_join(t1, NULL); pthread_join(t2, NULL); printf( all pthreads finished. ); return 0; }

You can compile the above code like so: $ cc spinlock.c -lpthread

= CPU utilization =

In the above code, the second thread immediately goes to sleep for 60 seconds. Meanwhile, the first thread checks repeatedly if the second thread has changed the value of i.

You can use the top or uptime utility found on Unix-like operating systems to see how this program utilizes the CPU. Run the program like this: $ uptime; ./a.out ; uptime 13:25:47 up 53 days, 23:50, 4 users, load average: 0.00, 0.00, 0.00 t2 changing the value of i to 99. i s value has changed to 99. all pthreads finished. 13:26:47 up 53 days, 23:51, 4 users, load average: 0.75, 0.21, 0.07

Of course, every system will return slightly different numbers, but the important thing to notice is that before we ran the program, the system load average for the previous 60 seconds was 0.00. After the program ran, the system load average bumped up to 0.75 for the last minute.

= Alternatives to busy waiting =

One alternative is using signal (computing). Most operating systems and thread libraries have support for putting the first thread into a sleep state, to be woken up later by a signal caused by the second thread changing the value of i. This technique. known as event-driven programming, is more efficient because the thread consumes no CPU cycles while it s asleep.

Busy waiting itself can be made much less wasteful by using a delay function found on most operating systems. This puts a thread to sleep for a specified time, during which the thread will waste no CPU time. If the loop is checking something simple then it will spend most of its time asleep and will not waste a large proportion of the available CPU time. It will still consume some CPU time though.

=See also=

*synchronization *spinlock *delay loop

=External links=

*[http://www.opengroup.org/onlinepubs/009695399/functions/pthread_spin_lock.html Description] from The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition *[http://citeseer.ist.psu.edu/cisq=spin+and+lock Citations from CiteSeer] *Article [http://codeproject.com/threads/spinlocks.asp User-Level Spin Locks - Threads, Processes & IPC] by Gert Boddaert *Course [http://www.cs.brown.edu/courses/cs176/spin.pdf Introduction to Multiprocessors: Algorithms, Data Structures, and Programming - Spin Locks and Contention] by Maurice Herlihy and Nir Shavit *[http://austria.sourceforge.net/dox/html/classSpinLock.html Austria SpinLock Class Reference]