<span id="Label3"></p><p><p><strong>1</strong> <strong>, overview</strong></p></p><p><p>(1) Run Loop provides a mechanism for executing code asynchronously and cannot perform tasks in Parallel.</p></p><p><p>(2) in the main queue, main run loop works directly with the execution of the task, handling UI events, timers, and other kernel-related Events.</p></p><p><p>(3) The main purpose of the run loop is to ensure that the thread that executes the program is not terminated by the System.</p></p><p><p><strong>Run Loop</strong> <strong>characteristics of the Work:</strong></p></p><p><p>(1) when an event occurs, the Run Loop notifies the application to respond based on the specific event Type.</p></p><p><p>(2) when no event occurs, the Run loop goes into hibernation to save Power.</p></p><p><p>(3) when the event occurs again, the Run loop is woken up to handle the Event.</p></p><p><p><strong>the main thread and other threads in the</strong> <strong>Run Loop</strong> <strong>:</strong></p></p><p><p>The main thread of the iOS program is already configured with Run loop by default, and other threads do not have the run loop set by Default.</p></p><p><p>Typically in development, Runloop are rarely actively created, and events are usually added to Runloop.</p></p><p><p><strong>Loop</strong> <strong>:</strong></p></p><p><p></p></p><p><p><strong>2</strong> <strong>,</strong> <strong>uiapplication</strong> <strong>in the</strong> <strong>Run Loop</strong></p></p><p><p><strong></strong></p></p><p><p><strong>3</strong> <strong>,</strong> <strong>circular references in multi-threading</strong></p></p><p><p>A circular reference can be caused if the Self object holds a reference to an operand, and the manipulation object directly accesses Self.</p></p><p><p>Using self in a manipulation object alone does not cause circular references.</p></p><p><p>Note: not available at this time (weakself)</p></p><p><p><strong>4</strong> <strong>, resource sharing in multiple threads</strong></p></p><p><p>Many of the problems in concurrent programming are rooted in accessing shared resources in multiple THREADS. A resource can be a property, an object, a network device, or a file.</p></p><p><p>Any shared resource in multiple threads can be a potential conflict point and must be carefully designed to prevent this conflict from Occurring.</p></p><p><p><strong>Resource Sharing Examples:</strong></p></p><p><p><strong></strong></p></p><p><p><strong>5</strong> <strong>,</strong> <strong>Mutual exclusion Lock</strong> <strong>(@synchronized)</strong></p></p><p><p>Mutual exclusion Lock Use note:</p></p><p><p>(1) plus mutex, add mutex in "read" "write" scope of shared resource</p></p><p><p>(2) to make the range of the lock as small as possible!</p></p><p><p>(3) resource looting doing the simple thing is to use the mutex lock @synchronized</p></p><p><p>(4) the use of mutual exclusion lock, will be slow, the cost of mutual exclusion lock is very high!</p></p><p><p></p></p><p><p>Plus a mutex allows a resource to be accessed only by one thread at a time, and only when the resource is exhausted by the Thread.</p></p><p><p>Mutex usage:</p></p><p><p>@synchronized (self) {</p></p><p><p>Threading Operations</p></p><p><p>}</p></p><p><p>For example:</p></p><p><p>@interface Mjviewcontroller ()</p></p><p><p>@property (weak, nonatomic) iboutlet uitextview *infotext;</p></p><p><p>The number of votes, <strong>if the use of atomic lock, just on the basis of mutual exclusion lock to</strong> <strong></strong> <strong>Change</strong> The following nonatomic <strong>atomic</strong> <strong>can</strong> be</p></p><p><p>@property (nonatomic, Assign) Nsinteger tickets;</p></p><p><p>@end</p></p><p><p></p></p><p><p>@implementation Mjviewcontroller</p></p><p><p></p></p><p><p>-(void) Viewdidload</p></p><p><p>{</p></p><p><p>[super viewdidload];</p></p><p><p>}</p></p><p><p></p></p><p><p>The circulation of the tickets has been sold out so far</p></p><p><p>-(void) dosaleloop: (nsstring *) opname</p></p><p><p>{</p></p><p><p>All threads are allowed to circulate and sell Tickets.</p></p><p><p>While (YES) {</p></p><p><p></p></p><p><p>@synchronized (self) {</p></p><p><p>If (self.tickets > 0) {</p></p><p><p>--self.tickets;</p></p><p><p></p></p><p><p>NSLog (@ "number of votes left%d-%@-%@", self.tickets, opname, [nsthread currentthread]);</p></p><p><p>} else {</p></p><p><p>Break</p></p><p><p>}</p></p><p><p>}</p></p><p><p>//-----------------------------------------</p></p><p><p>Simulated hibernation, unrelated to resource looting, not in a lock.</p></p><p><p>If ([opname isequaltostring:@ "OP 1"]) {</p></p><p><p>[nsthread sleepfortimeinterval:1.0f];</p></p><p><p>} else {</p></p><p><p>[nsthread sleepfortimeinterval:0.3f];</p></p><p><p>}</p></p><p><p>}</p></p><p><p>}</p></p><p><p></p></p><p><p>#pragma mark Mock multiplayer sell ticket</p></p><p><p>#pragma Mark Gcd realizes</p></p><p><p></p></p><p><p>-(ibaction) dosale: (id) Sender</p></p><p><p>{</p></p><p><p>Do multi-threaded start, never trust the results of a run</p></p><p><p>Self.tickets = 20;</p></p><p><p></p></p><p><p>1. Queue</p></p><p><p>dispatch_queue_t q = dispatch_queue_create ("sale", dispatch_queue_concurrent);</p></p><p><p></p></p><p><p>2. Add a task</p></p><p><p>Dispatch_async (q, ^{</p></p><p><p>[self dosaleloop:@ "OP 1"];</p></p><p><p>});</p></p><p><p></p></p><p><p>Dispatch_async (q, ^{</p></p><p><p>[self dosaleloop:@ "OP 2"];</p></p><p><p>});</p></p><p><p></p></p><p><p>Dispatch_async (q, ^{</p></p><p><p>[self dosaleloop:@ "OP 3"];</p></p><p><p>});</p></p><p><p></p></p><p><p>Dispatch_async (q, ^{</p></p><p><p>[self dosaleloop:@ "OP 4"];</p></p><p><p>});</p></p><p><p>}</p></p><p><p></p></p><p><p>@end</p></p><p><p>Note: There is also a lock feature in ios, atomic lock-a read-write lock (128-bit spin lock), which also consumes performance.</p></p><p><p>The atomic lock only protects the data when it is written, while reading is not responsible.</p></p><p><p>For the resources to write, it is important to protect the correctness of the write data, or the accuracy of the reads!</p></p><p><p>If you just develop a single-write multi-read feature, you only need to use an atomic lock.</p></p><p><p><strong>@synchronized</strong> <strong>performance consumption is very large, Apple is not recommended for official Use. </strong></p></p><p><p><strong>In the actual development, do not Rob resources!</strong></p></p><p><p>The main purpose of concurrent programming is to improve performance, to allow more code to run concurrently, to achieve concurrency and improve overall performance!</p></p><p><p>Mobile phone development is the most important is smooth, parallel, as for the resource Rob function development is a service-side category!</p></p><p><p></p></p><p><p>The run Loop for iOS development</p></p></span>
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.