<s>
In	O
computer	B-General_Concept
science	I-General_Concept
and	O
software	B-General_Concept
engineering	I-General_Concept
,	O
busy-waiting	B-Operating_System
,	O
busy-looping	O
or	O
spinning	O
is	O
a	O
technique	O
in	O
which	O
a	O
process	B-Operating_System
repeatedly	O
checks	O
to	O
see	O
if	O
a	O
condition	O
is	O
true	O
,	O
such	O
as	O
whether	O
keyboard	B-Device
input	O
or	O
a	O
lock	B-Operating_System
is	O
available	O
.	O
</s>
<s>
In	O
most	O
cases	O
spinning	O
is	O
considered	O
an	O
anti-pattern	O
and	O
should	O
be	O
avoided	O
,	O
as	O
processor	O
time	O
that	O
could	O
be	O
used	O
to	O
execute	O
a	O
different	O
task	B-Operating_System
is	O
instead	O
wasted	O
on	O
useless	O
activity	O
.	O
</s>
<s>
Spinning	O
can	O
be	O
a	O
valid	O
strategy	O
in	O
certain	O
circumstances	O
,	O
most	O
notably	O
in	O
the	O
implementation	O
of	O
spinlocks	B-Operating_System
within	O
operating	O
systems	O
designed	O
to	O
run	O
on	O
SMP	B-Operating_System
systems	O
.	O
</s>
<s>
The	O
following	O
C	B-Language
code	O
examples	O
illustrate	O
two	O
threads	B-Operating_System
that	O
share	O
a	O
global	O
integer	O
i	O
.	O
</s>
<s>
The	O
first	O
thread	B-Operating_System
uses	O
busy-waiting	B-Operating_System
to	O
check	O
for	O
a	O
change	O
in	O
the	O
value	O
of	O
i	O
:	O
</s>
<s>
Most	O
operating	O
systems	O
and	O
threading	O
libraries	O
provide	O
a	O
variety	O
of	O
system	B-Operating_System
calls	I-Operating_System
that	O
will	O
block	O
the	O
process	B-Operating_System
on	O
an	O
event	O
,	O
such	O
as	O
lock	B-Operating_System
acquisition	O
,	O
timer	O
changes	O
,	O
I/O	B-General_Concept
availability	O
or	O
signals	B-Operating_System
.	O
</s>
<s>
Using	O
such	O
calls	O
generally	O
produces	O
the	O
simplest	O
,	O
most	O
efficient	O
,	O
fair	O
,	O
and	O
race-free	O
result	O
.	O
</s>
<s>
A	O
single	O
call	O
checks	O
,	O
informs	O
the	O
scheduler	O
of	O
the	O
event	O
it	O
is	O
waiting	O
for	O
,	O
inserts	O
a	O
memory	B-General_Concept
barrier	I-General_Concept
where	O
applicable	O
,	O
and	O
may	O
perform	O
a	O
requested	O
I/O	B-General_Concept
operation	I-General_Concept
before	O
returning	O
.	O
</s>
<s>
The	O
scheduler	O
is	O
given	O
the	O
information	O
needed	O
to	O
implement	O
priority	B-Operating_System
inheritance	I-Operating_System
or	O
other	O
mechanisms	O
to	O
avoid	O
starvation	B-Operating_System
.	O
</s>
<s>
Busy-waiting	B-Operating_System
itself	O
can	O
be	O
made	O
much	O
less	O
wasteful	O
by	O
using	O
a	O
delay	O
function	O
(	O
e.g.	O
,	O
sleep( )	O
)	O
found	O
in	O
most	O
operating	O
systems	O
.	O
</s>
<s>
This	O
puts	O
a	O
thread	B-Operating_System
to	O
sleep	O
for	O
a	O
specified	O
time	O
,	O
during	O
which	O
the	O
thread	B-Operating_System
will	O
waste	O
no	O
CPU	O
time	O
.	O
</s>
<s>
In	O
programs	O
that	O
never	O
end	O
(	O
such	O
as	O
operating	O
systems	O
)	O
,	O
infinite	O
busy	B-Operating_System
waiting	I-Operating_System
can	O
be	O
implemented	O
by	O
using	O
unconditional	O
jumps	O
as	O
shown	O
by	O
this	O
NASM	B-Application
syntax	O
:	O
jmp	B-Device
$	O
.	O
</s>
<s>
The	O
CPU	O
will	O
unconditionally	O
jump	B-Device
to	O
its	O
own	B-General_Concept
position	I-General_Concept
forever	O
.	O
</s>
<s>
A	O
busy	B-Operating_System
wait	I-Operating_System
like	O
this	O
can	O
be	O
replaced	O
with	O
:	O
</s>
<s>
For	O
more	O
information	O
,	O
see	O
HLT	B-Language
(	O
x86	O
instruction	O
)	O
.	O
</s>
<s>
In	O
low-level	O
programming	O
,	O
busy-waits	B-Operating_System
may	O
actually	O
be	O
desirable	O
.	O
</s>
