Does using nsoperation in a single thread of iOS cause resources to compete?

Source: Internet
Author: User

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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.