Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads are slow when audio is off

Tags:

c++

audio

mfc

I have 2 projects. One is built by C++ Builder without MFC Style. And other one is VC++ MFC 11.

When I create a thread and create a cycle -- let's say this cycle adds one to progressbar position -- from 1 to 100 by using Sleep(10) it works of course for both C++ Builder and C++ MFC.

Now, Sleep(10) is wait 10 miliseconds. OK. But the problem is only if I have open media player, Winamp or anything else that produces "Sound". If I close all media player, winamp and other sound programs, my threads get slower than 10 miliseconds.

It takes like 50-100 ms / each. If I open any music, it works normally as I expected.

I have no any idea why this is happening. I first thought that I made a mistake inside MFC App but why does C++ Builder also slow down?

And yes, I am positively sure it is sound related because I even re-formated my windows, disabled everything. Lastly I discovered that sound issue.

Does my code need something?

Update:

Now, I follow the code and found that I used Sleep(1) in such areas to wait 1 miliseconds. The reason of this, I move an object from left to right. If I remove this sleep then the moving is not showing up because it is very fast. So, I should use Sleep(1). With Sleep(1), if audio is on than it works. If audio is off than it is very slow.

for (int i = 0; i <= 500; i++) {
   theDialog->staticText->SetWindowsPosition(NULL, i, 20, 0, 0);
   Sleep(1);
}

So, suggestions regarding this are really appreciated. What should I do?

I know this is the incorrect way. I should use something else that is proper and valid. But what exactly? Which function or class help me to move static texts from one position to another smoothly?

Also, changing the thread priority has not helped.

Update 2:

Update 1 is an another question :)

like image 900
xangr Avatar asked Dec 27 '12 06:12

xangr


1 Answers

Sleep (10), will (as we know), wait for approximately 10 milliseconds. If there is a higher priority thread which needs to be run at that moment, the thread wakeup maybe delayed. Multimedia threads are probably running in a Real-Time or High priority, as such when you play sound, your thread wakeup gets delayed.

Refer to Jeffrey Richters comment in Programming Applications for Microsoft Windows (4th Ed), section Sleeping in Chapter 7:

The system makes the thread not schedulable for approximately the number of milliseconds specified. That's right—if you tell the system you want to sleep for 100 milliseconds, you will sleep approximately that long but possibly several seconds or minutes more. Remember that Windows is not a real-time operating system. Your thread will probably wake up at the right time, but whether it does depends on what else is going on in the system.

Also as per MSDN Multimedia Class Scheduler Service (Windows)

MMCSS ensures that time-sensitive processing receives prioritized access to CPU resources.

As per the above documentation, you can also control the percentage of CPU resources that will be guaranteed to low-priority tasks, through a registry key

like image 152
stamhaney Avatar answered Oct 25 '22 08:10

stamhaney