ios - How does dispatch_set_target_queue work? -
due lack of material on dispatch_set_target_queue, have come here help, thanks!
here test code:
dispatch_queue_t myserialdispatchqueue1 = dispatch_queue_create("come.itenyh", null); dispatch_queue_t myserialdispatchqueue2 = dispatch_queue_create("come.itenyh1", null); dispatch_set_target_queue(myserialdispatchqueue1, myserialdispatchqueue2); dispatch_async(myserialdispatchqueue1, ^{[self task:@"s1"];}); dispatch_async(myserialdispatchqueue2, ^{[self task:@"p1"];}); dispatch_async(myserialdispatchqueue1, ^{[self task:@"s2"];}); - (void)task:(nsstring *)taskid { nslog(@"now executing taskid:%@", taskid); [nsthread sleepfortimeinterval:5]; } now if set
dispatch_set_target_queue(myserialdispatchqueue2, myserialdispatchqueue1); then result is:
2014-04-16 22:23:49.581 itgcdlearning[66758:1303] executing taskid:s1 2014-04-16 22:23:54.585 itgcdlearning[66758:1303] executing taskid:s2 2014-04-16 22:23:59.586 itgcdlearning[66758:1303] executing taskid:p1 and on contrary, if set
dispatch_set_target_queue(myserialdispatchqueue1, myserialdispatchqueue2); then result is:
2014-04-16 22:28:37.910 itgcdlearning[66795:1303] executing taskid:s1 2014-04-16 22:28:42.913 itgcdlearning[66795:1303] executing taskid:p1 2014-04-16 22:28:47.915 itgcdlearning[66795:1303] executing taskid:s2 i know dispatch_set_target_queue function can
- change priorty of queue
- create hierarchy of dispatch queues.
i think second point leads result in code, don't know specific details. can explain me?
according me when set target queue other synchronizing task of both queue in first case: dispatch_set_target_queue(myserialdispatchqueue2, myserialdispatchqueue1); myserialdispatchqueue1 target queue task added in myserialdispatchqueue2 enqueued myserialdispatchqueue1. target queue.so myserialdispatchqueue1 has 2 task other 1 queue myserialdispatchqueue2 added later.
in second case: dispatch_set_target_queue(myserialdispatchqueue1, myserialdispatchqueue2); target queue myserialdispatchqueue2 in start when there no task task myserialdispatchqueue1 added in myserialdispatchqueue2 own task in queue. in way task has been added.
Comments
Post a Comment