multithreading - Sleep a long running process -
i have app iterates on tens of thousands of records using various enumerators (such directory enumerators)
i seeing os x saying process "caught burning cpu" since taking large amount of cpu in doing so.
what build in "pressure valve" such a
[nsthread sleepfortimeinterval:cpudelay];
that not block other processes/threads on things dual core machine.
my processing happening on separate thread, can't break out of , re-enter enumerator loop , use nstimers allow machine "breathe"
any suggestions - should [nsthread sleepfortimeinterval:cpudelay];
working?
i run stuff inside dispatch queue:
if(!primarytask)primarytask=dispatch_queue_create( "com.me.app.task1",backgroundpriorityattr); dispatch_async(primarytask,^{ [self dosync]; });
try wrapping processing in nsoperation , set lower qos priority. here little more information: http://nshipster.com/nsoperation/
here code example made up. operation triggered in view load event:
let processingqueue = nsoperationqueue() override func viewdidload() { let backgroundoperation = nsblockoperation { //my long , intesive processing let npoints = 100000000000 var npointsinside = 0 _ in 1...npoints { let (x, y) = (drand48() * 2 - 1, drand48() * 2 - 1) if x * x + y * y <= 1 { npointsinside += 1 } } let _ = 4.0 * double(npointsinside) / double(npoints) } backgroundoperation.queuepriority = .low backgroundoperation.qualityofservice = .background processingqueue.addoperation(backgroundoperation) }
Comments
Post a Comment