IOS themes await action

I have a processing thread that I use to populate a data buffer. Elsewhere, a piece of hardware calls a callback that is read from this data buffer. Then the processing thread starts and fills the buffer.

When the buffer is full, I am currently saying the thread is waiting:

while( [self FreeWriteSpace] < mProcessBufferSize && InActive) { [NSThread sleepForTimeInterval:.0001]; } 

However, when I browse, I get a lot of processor time spent in a dream. Is there a better way to wait? I don’t even care if the profiles say that the time spent in a dream?

+4
source share
2 answers

The time spent in a dream is virtually free. In the "Tools" look at "running samples" and not "all samples". But this is not an ideal solution.

First off, your sleep mode is crazy. Do you really need literacy? 1μs? The system almost certainly does not give you, because the processor is not so fast. I have to believe that you could do this before .1 or .01. But this is still a busy expectation, which is not ideal if you can help him.

The best solution is to use NSCondition . In this thread, wait on the condition, and in your processing thread, activate the condition when there is space for recording.

Be careful with your name. Do not name methods with leading caps (this means that this is the name of the class). And avoid direct access to ivars (InActive). "InActive" is also a very confusing name. Does this mean that the system is active (in the active state) or inactive (inactive). Naming in Objective-C is extremely important. The compiler will not protect you the way it does in C # and C ++. A good name is how you keep your programs working, and many parts of ObjC rely on it.

You can also explore the Grand Central Dispatch, which is specifically designed for such problems. Take a look at dispatch_async() to work with the new data.

+8
source

However, when I profile, I get a lot of CPU time spent in a dream. Is there a better way to wait? Am I even indicated in profiles that I spent time in a dream?

Yes - never, never try. The survey eats the processor, makes your application less responsive, eats the battery and everything around the waste.

Notify instead.

The easiest way is to use one of the options “execute selector in the main thread” (see the NSThread documentation). Or send to the queue (including something like dispatch_async(dispatch_get_main_queue(), ^{ ... yo, data be ready ...}); ).

+2
source

All Articles