Talk:Busy waiting
This article is rated Start-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | ||||||||||||||||||
|
Volatile Reference
[edit]This page is referenced by the volatile variable page: "For an example of the use of volatile in context, see Busy waiting." However, volatile variable also mentions that "In C and C++, volatile was not intended, and it is not implemented on all systems, to be a correct or useful synchronization primitive." The code example on this page does seem to be using the volatile variable 'i' as a synchronization primitive; will the example code actually work all the time? If not, should we find a different primitive to use for synchronizing the example code? Since the example code already uses pthreads, maybe something from pthreads? 134.173.66.78 (talk) 02:59, 30 January 2009 (UTC)
- The reason for not using "volatile" as a synchronization primitive is that you can't count on memory reads or writes being atomic -- if you're looking for a transition from 0xFFFF to 0xFF00, and the other process writes 0x0000 in two byte-sized chunks, you might get a false positive depending on the order in which the bytes are written. In the example in the article, it's not wrong, merely bad practice. --Carnildo (talk)
- Volatile does NOT enforce atomic variable access in C and C++. The code is just *wrong* — Preceding unsigned comment added by Axelgneiting (talk • contribs) 17:28, 8 February 2011 (UTC)
- Specifically, multi-core machines (which are now ubiquitous) negate the assumption that even single-byte reads and writes are atomic, as the processors do not necessarily share caches. —Preceding unsigned comment added by 68.103.216.128 (talk) 01:44, 25 March 2011 (UTC)
- Volatile does NOT enforce atomic variable access in C and C++. The code is just *wrong* — Preceding unsigned comment added by Axelgneiting (talk • contribs) 17:28, 8 February 2011 (UTC)
This code is broken. (It would be bad practice even if it were correct, which it is not.) There is no operation in C++ that is guaranteed atomic. One must either use the atomic types defined in the standard library of C++11, or if those are not yet available to you, roll your own using operating-system specific routines like InterlockedIncrement and InterlockedExchange on Windows. The purpose of "volatile" is to tell the compiler not to optimize away operations on the variable which appear to be redundant. Jive Dadson (talk) 03:06, 19 August 2012 (UTC)
Polling Discussion and interrupts
[edit]Discussing shortly the concepts polling (computer science) and Interrupt would be a great addition to this article, in my eyes, as they bear similar issues. --Abdull 17:42, 7 November 2005 (UTC)
- I added the merge suggestion as the polling and busy waiting articles don't explain the differences between both techniques. --Abdull 00:53, 22 November 2006 (UTC)
- This is programming jargon. While the concepts may be similar, the terminology is used for specific circumstances. Busy waiting generally applies to cpu scheduling, while polling refers mainly to input devices. It simply does not make sense to say that you "polled the cpu" or "busy waited on the mouse".
Example
[edit]nice to see you, 'while true do skip':
while (i==0) {}
Non-Unix Explanation
[edit]Someone needs to explain the use of uptime for non-Unix folks. 218.102.220.129 12:17, 24 May 2006 (UTC)
Another reason the code is broken
[edit]I just noticed another bug besides the ones already pointed out. Maybe this is what the terse "Example" above was implying. If the thread that is supposed to set i to something other than 0 (the "writer") happens to be running on the same core as the one that checks the variable, the polling loop can lock out the writer forever. Many systems give all the threads some time automatically, but that is by no means guaranteed. — Preceding unsigned comment added by Jive Dadson (talk • contribs) 03:52, 19 August 2012 (UTC)
Suggestion for a correct new example
[edit]Someone who has the time could find an example in the Boost library. The copyright for Boost would allow its inclusion here provided there is a link to the copyright itself. Boost is very well tested.
In the meantime, the current example should be removed. Jive Dadson (talk) 04:21, 19 August 2012 (UTC)
Another error - variable not stored
[edit]Yet another error in the example: the variable i
value is not stored in f1
, so if the writer makes two assignments in short time
i = 99;
....
i = 0;
then the reader may notice the change, but print-out that the value changed from zero to ...zero.
It should rather be something like
static void *f1(void *p)
{
int i_copy;
while ((i_copy = i)==0) {
/* do nothing - just keep checking over and over */
}
printf("i's value has changed to %d.\n", i_copy);
return NULL;
}
Of course that does not guarantee the reader will notice the change, however if it does, it will at least print the correct message. --CiaPan (talk) 07:04, 6 August 2015 (UTC)
Why is this busy?
[edit]There is a sleep call in the loop. Why the claim 'wasting CPU resources' then? What is busy in it? Seems to me this is polling. — Preceding unsigned comment added by Midiparse (talk • contribs) 16:35, 17 January 2017 (UTC)
- @Midiparse: There are three threads operating at the same time:
f1()
is the one wasting CPU resources:while (i==0) {}
will poll the variablei
in an infinite loop with no delay, using 100% of its allowed CPU resources, untilf2()
(which sleeps for 60 seconds and takes no CPU resources) wakes up independently and changesi
.main()
just launches f1() and f2() then waits for them withpthread_join()
, so it's sleeping also until f1() and f2() end. --Closeapple (talk) 10:04, 19 January 2017 (UTC)
Busy waiting on microcontrollers
[edit]Should there be some mention of microcontrollers in this article?
It seems busy waiting is fairly common on microcontrollers, where it's not considered an anti-pattern because the power usage is not significantly increased and there are no other threads/processes to run or take processing resources from. Here's an example of discussion about a busy wait used just to halt a program on Arduino. Polling and other similar busy wait style loops are also common and generally don't seem to be considered bad form. —Pengo 01:32, 20 March 2017 (UTC)
External links modified
[edit]Hello fellow Wikipedians,
I have just modified 2 external links on Busy waiting. Please take a moment to review my edit. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit this simple FaQ for additional information. I made the following changes:
- Added archive https://web.archive.org/web/20131005133753/https://www.kernel.org/doc/Documentation/volatile-considered-harmful.txt to https://www.kernel.org/doc/Documentation/volatile-considered-harmful.txt
- Added archive https://web.archive.org/web/20041211235628/http://www.codeproject.com/threads/spinlocks.asp to http://codeproject.com/threads/spinlocks.asp
When you have finished reviewing my changes, you may follow the instructions on the template below to fix any issues with the URLs.
This message was posted before February 2018. After February 2018, "External links modified" talk page sections are no longer generated or monitored by InternetArchiveBot. No special action is required regarding these talk page notices, other than regular verification using the archive tool instructions below. Editors have permission to delete these "External links modified" talk page sections if they want to de-clutter talk pages, but see the RfC before doing mass systematic removals. This message is updated dynamically through the template {{source check}}
(last update: 5 June 2024).
- If you have discovered URLs which were erroneously considered dead by the bot, you can report them with this tool.
- If you found an error with any archives or the URLs themselves, you can fix them with this tool.
Cheers.—InternetArchiveBot (Report bug) 02:35, 15 December 2017 (UTC)
As a time-delay technique
[edit]Back in the day, a lot of MS-DOS games (probably mostly shareware/freeware/PD) used spinning as a time-delay technique, with the consequence that they would run at the correct speed only on a CPU of the same speed as the one they were built and tested on.
But there is something else I'm wondering. Do some compilers just optimise away a do-nothing loop, such that there will be no time delay at all? — Smjg (talk) 09:29, 5 June 2024 (UTC)