For win16 programs, Windows implemented cooperative multitasking. Its implementation was based upon the "message loop" architecture of every Windows program.
The duty of every program was to endlessly run in a loop in which a call to GetMessage()
function was performed. This function call looks whether a message to this process is in the queue. If there is one, it is retrieved (GetMessage
), optionally translated (TranslateMessage
, this is done to convert keyboard shortcuts into menu actions) and finally, passed to the window procedure (DispatchMessage
).
If there is no message available in the queue, Windows suspends the current task and gives the CPU to other task, which will try to retrieve a message from its queue as well, yielding the CPU to another task if no message is present.
If a program needed to perform background tasks while there was no pending messages in its queue, it would call PeekMessage
instead of GetMessage
. This didn't make the task to relinquish the CPU, as PeekMessage
would inmediately return to the caller task informing whether a message is available or not. This was used to time-multiplex message processing with another time-consuming task (think of a 3D program doing a render, but allowing the user to cancel that render by pressing a "Cancel" button).
If this time-consumning task was actually very time-consuming, a well-behaved program should call the yield()
function from time to time in order to relinquish the CPU and let other task to run.
A bad-behaved program could easily hog the CPU by not retrieving messages too often, or spending too much time in a window procedure function.
The situation was quite different for MS DOS boxes. They ran using the V8086 mode of the 80386 (if Windows was running in enhanced mode). Windows-aware programs ran in the so-called VM 0 (virtual machine 0). DOS boxes ran from VM 1 upwards.
DOS programs usually were bad-behaved programs, so Windows assigned each DOS box a different virtual machine. Virtual machines used preemptive multitasking in Windows 3.1 and thus, each DOS box could run concurrently of each others, and concurrently of any Windodws-aware program.
history
tag on The Old New Thing. – Michael Kjörling Jun 8 '16 at 13:37