Knowledge

Busy waiting

Source 📝

822:
fetch device status resulting from the write operation, status that may not become valid until a number of machine cycles have elapsed following the write. The programmer could call an operating system delay function, but doing so may consume more time than would be expended in spinning for a few clock cycles waiting for the device to return its status.
62:
is available. Spinning can also be used to generate an arbitrary time delay, a technique that was necessary on systems that lacked a method of waiting a specific length of time. Processor speeds vary greatly from computer to computer, especially as some processors are designed to dynamically adjust
821:
In low-level programming, busy-waits may actually be desirable. It may not be desirable or practical to implement interrupt-driven processing for every hardware device, particularly those that are seldom accessed. Sometimes it is necessary to write some sort of control data to hardware and then
765:) found in 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 waste very little CPU time. 63:
speed based on current workload. Consequently, spinning as a time-delay technique can produce inconsistent or even unpredictable results on different systems unless code is included to determine the time a processor takes to execute a "do nothing"
748:
where applicable, and may perform a requested I/O operation before returning. Other processes can use the CPU while the caller is blocked. The scheduler is given the information needed to implement
899: 934: 768:
In programs that never end (such as operating systems), infinite busy waiting can be implemented by using unconditional jumps as shown by this
881: 942: 82:
is instead wasted on useless activity. Spinning can be a valid strategy in certain circumstances, most notably in the implementation of
930: 713: 856: 895: 923: 966: 831: 709: 87: 79: 17: 744:-free result. A single call checks, informs the scheduler of the event it is waiting for, inserts a 99: 861: 59: 961: 939: 804: 777: 749: 35: 8: 753: 51: 737: 729: 851: 769: 55: 759:
Busy-waiting itself can be made much less wasteful by using a delay function (e.g.,
836: 78:
and should be avoided, as processor time that could be used to execute a different
31: 946: 781: 68: 64: 740:. Using such calls generally produces the simplest, most efficient, fair, and 745: 741: 926:
from The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition
148:/* i is global, so it is visible to all functions. It makes use of the special 955: 109:. The first thread uses busy-waiting to check for a change in the value of 75: 214:/* Atomically load current value of i into local_i and check if that value 725: 724:
Most operating systems and threading libraries provide a variety of
846: 841: 816: 83: 732:
the process on an event, such as lock acquisition, timer changes,
761: 103: 54:
repeatedly checks to see if a condition is true, such as whether
733: 102:
code examples illustrate two threads that share a global
172:/* f1 uses a spinlock to wait for i to change from 0. */ 151:* type atomic_int, which allows atomic memory accesses. 784:
forever. A busy wait like this can be replaced with:
931:
User-Level Spin Locks - Threads, Processes & IPC
259:/* do nothing - just keep checking over and over */ 896:"Why the 'volatile' type class should not be used" 953: 708:In a use case like this, one can consider using 27:Continuously checking a condition in computing 86:within operating systems designed to run on 388:"t2 has changed the value of i to %d. 67:, or the looping code explicitly checks a 74:In most cases spinning is considered an 14: 954: 271:"i's value has changed to %d. 810: 24: 857:Synchronization (computer science) 688:"All pthreads finished." 25: 978: 917: 93: 940:Austria SpinLock Class Reference 902:from the original on 2017-10-04 776:. The CPU will unconditionally 719: 888: 882:"Intel Turbo Boost Technology" 874: 13: 1: 867: 752:or other mechanisms to avoid 7: 825: 10: 983: 832:Polling (computer science) 814: 803:For more information, see 358:/* sleep for 10 seconds */ 50:is a technique in which a 786: 115: 622:"pthread f2 failed 526:"pthread f1 failed 805:HLT (x86 instruction) 862:Peterson's algorithm 750:priority inheritance 36:software engineering 967:Concurrency control 714:condition variables 127:<stdatomic.h> 945:2011-05-14 at the 852:volatile variable 811:Appropriate usage 121:<pthread.h> 16:(Redirected from 974: 911: 910: 908: 907: 892: 886: 885: 878: 837:Non-blocking I/O 799: 796: 793: 790: 764: 736:availability or 704: 701: 698: 695: 692: 689: 686: 683: 680: 677: 674: 671: 668: 665: 662: 659: 656: 653: 650: 647: 644: 641: 638: 635: 632: 629: 626: 623: 620: 617: 614: 611: 608: 605: 602: 599: 596: 593: 590: 587: 584: 581: 578: 575: 572: 569: 566: 563: 560: 557: 554: 551: 548: 545: 542: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 494: 491: 488: 485: 482: 479: 476: 473: 470: 467: 464: 461: 458: 455: 452: 449: 446: 443: 440: 437: 434: 431: 428: 425: 422: 419: 416: 413: 410: 407: 404: 401: 398: 395: 392: 389: 386: 383: 380: 377: 374: 371: 368: 365: 362: 359: 356: 353: 350: 347: 344: 341: 338: 335: 332: 329: 326: 323: 320: 317: 314: 311: 308: 305: 302: 299: 296: 293: 290: 287: 284: 281: 278: 275: 272: 269: 266: 263: 260: 257: 254: 251: 248: 245: 242: 239: 236: 233: 230: 227: 224: 221: 218: 215: 212: 209: 206: 203: 200: 197: 194: 191: 188: 185: 182: 179: 176: 173: 170: 167: 164: 161: 158: 155: 152: 149: 146: 145:<unistd.h> 143: 140: 139:<stdlib.h> 137: 134: 131: 128: 125: 122: 119: 32:computer science 21: 982: 981: 977: 976: 975: 973: 972: 971: 952: 951: 947:Wayback Machine 920: 915: 914: 905: 903: 894: 893: 889: 880: 879: 875: 870: 828: 819: 813: 801: 800: 797: 794: 791: 788: 775: 760: 722: 706: 705: 702: 699: 696: 693: 690: 687: 684: 681: 678: 675: 672: 669: 666: 663: 660: 657: 654: 651: 648: 645: 642: 639: 636: 633: 630: 627: 624: 621: 618: 615: 612: 609: 606: 603: 600: 597: 594: 591: 588: 585: 582: 579: 576: 573: 570: 567: 564: 561: 558: 555: 552: 549: 546: 543: 540: 537: 534: 531: 528: 525: 522: 519: 516: 513: 510: 507: 504: 501: 498: 495: 492: 489: 486: 483: 480: 477: 474: 471: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 426: 423: 420: 417: 414: 411: 408: 405: 402: 399: 396: 393: 390: 387: 384: 381: 378: 375: 372: 369: 366: 363: 360: 357: 354: 351: 348: 345: 342: 339: 336: 333: 330: 327: 324: 321: 318: 315: 312: 309: 306: 303: 300: 297: 294: 291: 288: 285: 282: 279: 276: 273: 270: 267: 264: 261: 258: 255: 252: 249: 246: 243: 240: 237: 234: 231: 228: 225: 222: 219: 216: 213: 210: 207: 204: 201: 198: 195: 192: 189: 186: 183: 180: 177: 174: 171: 168: 165: 162: 159: 156: 153: 150: 147: 144: 141: 138: 135: 133:<stdio.h> 132: 129: 126: 123: 120: 117: 96: 69:real-time clock 28: 23: 22: 15: 12: 11: 5: 980: 970: 969: 964: 950: 949: 937: 927: 919: 918:External links 916: 913: 912: 887: 872: 871: 869: 866: 865: 864: 859: 854: 849: 844: 839: 834: 827: 824: 812: 809: 787: 773: 746:memory barrier 721: 718: 556:pthread_create 460:pthread_create 116: 98:The following 95: 94:Example C code 92: 26: 9: 6: 4: 3: 2: 979: 968: 965: 963: 962:Anti-patterns 960: 959: 957: 948: 944: 941: 938: 936: 935:Gert Boddaert 932: 928: 925: 922: 921: 901: 897: 891: 883: 877: 873: 863: 860: 858: 855: 853: 850: 848: 845: 843: 840: 838: 835: 833: 830: 829: 823: 818: 808: 806: 785: 783: 779: 771: 766: 763: 757: 755: 751: 747: 743: 739: 735: 731: 727: 717: 715: 711: 114: 112: 108: 105: 101: 91: 89: 85: 81: 77: 72: 70: 66: 61: 57: 53: 49: 45: 41: 37: 33: 19: 904:. Retrieved 890: 876: 820: 802: 782:own position 767: 758: 726:system calls 723: 720:Alternatives 707: 664:pthread_join 646:pthread_join 637:EXIT_FAILURE 541:EXIT_FAILURE 361:atomic_store 110: 106: 97: 76:anti-pattern 73: 47: 44:busy-looping 43: 40:busy-waiting 39: 29: 924:Description 232:atomic_load 58:input or a 956:Categories 906:2013-06-10 868:References 815:See also: 754:starvation 728:that will 217:is zero */ 157:atomic_int 929:Article " 439:pthread_t 90:systems. 84:spinlocks 18:Busy-wait 943:Archived 900:Archived 847:BogoMips 842:Spinlock 826:See also 817:Spinlock 772:syntax: 142:#include 136:#include 130:#include 124:#include 118:#include 56:keyboard 48:spinning 780:to its 762:sleep() 738:signals 610:fprintf 514:fprintf 400:local_i 376:local_i 334:local_i 283:local_i 226:local_i 208:local_i 104:integer 52:process 789:sleep: 774:jmp $ 694:return 634:return 628:" 616:stderr 538:return 532:" 520:stderr 406:return 394:" 382:printf 301:static 289:return 277:" 265:printf 175:static 933:" by 798:sleep 730:block 562:& 466:& 367:& 346:sleep 238:& 220:while 778:jump 770:NASM 742:race 682:puts 676:NULL 658:NULL 583:NULL 571:NULL 487:NULL 475:NULL 421:main 409:NULL 316:void 304:void 292:NULL 190:void 178:void 80:task 65:loop 60:lock 34:and 795:jmp 792:hlt 734:I/O 712:'s 710:C11 430:int 418:int 331:int 205:int 88:SMP 46:or 30:In 958:: 898:. 807:. 756:. 716:. 691:); 679:); 670:t2 661:); 652:t1 631:); 625:\n 598:!= 595:rc 589:if 586:); 577:f2 565:t2 550:rc 535:); 529:\n 502:!= 499:rc 493:if 490:); 481:f1 469:t1 454:rc 448:t2 442:t1 433:rc 424:() 403:); 391:\n 379:); 355:); 352:10 340:99 310:f2 286:); 274:\n 247:== 244:)) 223:(( 184:f1 154:*/ 113:: 71:. 42:, 38:, 909:. 884:. 703:} 700:; 697:0 685:( 673:, 667:( 655:, 649:( 643:} 640:; 619:, 613:( 607:{ 604:) 601:0 592:( 580:, 574:, 568:, 559:( 553:= 547:} 544:; 523:, 517:( 511:{ 508:) 505:0 496:( 484:, 478:, 472:, 463:( 457:= 451:; 445:, 436:; 427:{ 415:} 412:; 397:, 385:( 373:, 370:i 364:( 349:( 343:; 337:= 328:{ 325:) 322:p 319:* 313:( 307:* 298:} 295:; 280:, 268:( 262:} 256:{ 253:) 250:0 241:i 235:( 229:= 211:; 202:{ 199:) 196:p 193:* 187:( 181:* 169:; 166:0 163:= 160:i 111:i 107:i 100:C 20:)

Index

Busy-wait
computer science
software engineering
process
keyboard
lock
loop
real-time clock
anti-pattern
task
spinlocks
SMP
C
integer
C11
condition variables
system calls
block
I/O
signals
race
memory barrier
priority inheritance
starvation
sleep()
NASM
jump
own position
HLT (x86 instruction)
Spinlock

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.