Object-c Multithreading

Source: Internet
Author: User
Object-c multithreading is as convenient and reliable as Java multithreading.
First, thread creation and startup
There are two main ways to create threads: [CPP]View Plaincopyprint? -(ID) init; Designated initializer-(ID) Initwithtarget: (ID) Target selector: (SEL) Selector object: (ID) argument;
There is, of course, a special kind of use of the so-called convenient method, which can directly generate a thread and start it without being responsible for thread cleanup. The interface of this method is: [CPP]View Plain copy print? + (void) Detachnewthreadselector: (SEL) Aselector totarget: (ID) atarget withobject: (ID) anargument
After the first two methods are created, you need the phone to start, and the method to start is: [CPP]View Plain copy print? -(void) start;
Synchronization and locking of threads
To illustrate the thread synchronization and lock, the best example may be multiple windows at the same time ticketing system. We know that in Java, we use synchronized to synchronize, while the iphone does not provide a Java-like synchronized keyword, but provides a Nscondition object interface. Looking at Nscondition's interface instructions, we can see that nscondition is the lock object under the iphone, so we can use nscondition to implement thread safety in the iphone. This is an example from the Web:
SellTicketsAppDelegate.h file [CPP]View Plain copy print?   SellTicketsAppDelegate.h import @interface sellticketsappdelegate:nsobject {int tickets;   int count;   nsthread* Ticketsthreadone;   nsthread* Ticketsthreadtwo;   nscondition* ticketscondition;   UIWindow *window;   @property (nonatomic, retain) Iboutlet UIWindow *window; @end
SELLTICKETSAPPDELEGATE.M file [CPP]View Plain

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.