XP sleeps 500ms when calling PlaySound()?

Hi,

on different multicore PCs (I do not have this effect on single core machines.) Windows XP 32 bit (SP3) here behaves as follows:

I run a small programm occupying the CPU and printing the time for one calculation step. E.g.:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <chrono>
#include <iostream>
#include <vector>
void WasteTime()
{
    std::vector<int> vec( 16777216, 42 );
    for ( int i( 0 ); i < 8; ++i )
        for ( auto it( vec.begin() ); it != vec.end(); ++it )
            *it = (*it + 13)/2;
}
int main( int argc, char* argv[] )
{
    for (;;)
    {
        auto start(std::chrono::system_clock::now());
        WasteTime();
        auto end(std::chrono::system_clock::now());
        int elapsed_milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count();
        std::cout << "elapsed time in ms: " << elapsed_milliseconds << "\n";
    }
    return 0;
}

Output:
elapsed time in ms: 187
elapsed time in ms: 171
elapsed time in ms: 171
elapsed time in ms: 156
elapsed time in ms: 156
elapsed time in ms: 171
elapsed time in ms: 171
elapsed time in ms: 171
elapsed time in ms: 156
elapsed time in ms: 156
elapsed time in ms: 171
elapsed time in ms: 171
elapsed time in ms: 156
elapsed time in ms: 171
elapsed time in ms: 171

But if I play sounds in between, e.g. with clicking here ( http://daiw.de/share/PlaySound.jpg ), my program sometimes is put to sleep for 500ms:
elapsed time in ms: 171
elapsed time in ms: 156
elapsed time in ms: 156
elapsed time in ms: 171
elapsed time in ms: 171
elapsed time in ms: 656
elapsed time in ms: 171
elapsed time in ms: 171
elapsed time in ms: 156
elapsed time in ms: 656
elapsed time in ms: 171
elapsed time in ms: 656
elapsed time in ms: 156
elapsed time in ms: 171
elapsed time in ms: 171

The ugly lines (six hundred something) occur frequently when I hit the play button in a rapid succession.
Do you know this phenomenon of even its cause? If you have a XP PC there, could you check if your machine behaves the same way?
It sounds like your not playing the sound from the C ++ program. I'm going to guess on a single core, the processor takes turns running programs and the program you use to play the sound takes up 500ms of processor time.

or

The processor should swap back and forth between all programs running so that they seem to all run at the same time but in reality, there is only one processor, it's only able to do one task, it will swap back and forth between all programs running, which your seeing as a 500ms delay.

or

If your PlaySound() is part of the app your running, that may be how long it takes to play the sound and return the control to the program. try playing a sound that last longer and see how the elapsed time is affected.
Last edited on
Hi SamuelAdams,
surprisingly the behaviour only occurs to me on multicore processors, not on single core ones (see text above).
PlaySound is not part of my app. I just use the audio settings in the control panel of windows (see linked image above) to play the sound, and the 500ms are independent of the length of the audio file.

edit: If somebody wants to try to reproduce this effect, and wants to use my test-app but can not compile it, here is a link to the executable: http://daiw.de/share/PrintCalculationTimes.zip
Last edited on
Topic archived. No new replies allowed.