Today in dealing with multi-threaded suddenly think of a problem, multi-core processor will not cause, a single thread, by the runloop distribution of 2 functions simultaneously executed? And then modify the same variable at the same time, creating a bug?
I have done the following tests:
- (void) viewdidload {[Super viewdidload]; [Self Performselectoronmainthread: @selector (test1) Withobject:nil Waituntildone:no]; [Self Performselectoronmainthread: @selector (test2) Withobject:nil Waituntildone:no]; NSLog (@"view did load end!");}- (void) test1{ for(inti =0;i<10000; i++) {NSLog (@"1111111111111111111"); }}- (void) test2{ for(inti =0;i<10000; i++) {NSLog (@"222222222222222222"); }
}
The result is that after 11111 of all outputs, only 22222 of the output, visible multi-core processor, will also ensure that a single thread does not appear concurrent execution of 2 functions of the situation!
What is the use of this conclusion? You can look at the box's API, which is the network request is issued by the operationqueue operation, but the real business logic is in the only thread you create through Performselector execution, So no matter how many box network requests are issued, there will be no simultaneous modification of a count variable that I have defined, and this variable is safe!
Does using nsoperation in a single thread of iOS cause resources to compete?