4

I have a strange situation where a while loop is causing my cpu usage to go to between 90 and 100%. The cpu stays this high. If I comment out the while loop the cpu remains normal.

Whats going wrong here?

I've put in a breakpoint and the while loop definitely does exit.

[self performSelectorInBackground:@selector(checkstate:) withObject:padid];



-(void)checkstate:(PadIDSIdentifier*)pids
    {

        int pid=0;
        int cid=0;
        pid=pids.padid;
        cid=pids.channelid;


        NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];


        while (change==NO) 
        {

         // wait for the condition I want
         change=YES;




        }



        [pool release];

    }
dubbeat
  • 7,706
  • 18
  • 70
  • 122

3 Answers3

5

You're eating up the CPU in that loop. What you need to do is let the OS wait for you (which sets the wait process on low-priority so it occurs in idle time).

How you do that in Windows is WaitForSingleObject. How you do that on iPhone is with NSCondition.

Here is link: How do I use NSConditionLock? Or NSCondition

Basically, the NSCondition is signaled by the other thread, allowing your thread to resume processing.

Community
  • 1
  • 1
Lee Louviere
  • 5,162
  • 30
  • 54
  • perfect. NSTimer seems to work too. I'm concerned though that I dont understand the whole not exiting the "tight loop" thing though. – dubbeat Oct 26 '11 at 15:00
2

Your CPU is stuck in the loop. It's basically going round in circles 100% of the time waiting for the variable change to change to something else.

Deleted
  • 4,804
  • 1
  • 22
  • 17
  • but thats the strange thing. The value change "does" change its value and the loop does exit. – dubbeat Oct 26 '11 at 14:43
  • but before it has changed, it's in a "tight loop". – Deleted Oct 26 '11 at 14:45
  • I'm sorry but I don't understand. I totally get that while "change" false it's in a tight loop. What I don't get is when "change" changes to true it is no longer in a tight loop. – dubbeat Oct 26 '11 at 14:54
  • It's about priority. The % CPU doesn't mean if your CPU is being used or not. It means that a process is actively engaged in using the CPU for something. Processes / threads each get time-splices in which to operate. If the CPU is used up on active processes, another process might have to wait. However, if you let the OS wait for you, it does so in the background either on it's own slice. In other words, it puts the wait on super low priority, so that programs that need to use the CPU for something important can still do so. If your waiting actively in a loop, you're hogging the CPU. – Lee Louviere Oct 26 '11 at 15:02
  • It's like a sleep that the OS terminates early if a wait-object is signaled. – Lee Louviere Oct 26 '11 at 15:03
1

It doesn't look like the code you've shown accurately reflects the code you're running.

Any time your program is in control, it is using 100% CPU. The only way to use less is to return to the operating system. When you run a tight loop, it won't be going back to the OS and so it will use 100% CPU for as long as it runs.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622