<span id="Label3"></p>iOS three multithreading technology 1.NSThread each Nsthread object corresponds to one thread, the magnitude is lighter (real Multi-threaded) 2. The following two points are Apple's "concurrency" technology specifically developed to allow programmers to stop worrying about thread-specific usage issues ønsoper Ation/nsoperationqueue object-oriented Threading Technology Øgcd--grand Central Dispatch (distributed) is a c-based framework that can take full advantage of multicore and is the recommended multithreaded technology for Apple<p><p></p></p><p><p>The above three kinds of programming from top to bottom, the level of abstraction from low to high, the higher the abstraction of the more simple to use, is also the most recommended by apple, many of the framework technology in the project using different multithreading Technology.</p></p>2. Comparison of three multithreading techniques nsthread:– pros: nsthread is lighter than the other two lightweight, easy to use – cons: you need to manage your thread's life cycle, thread synchronization, locking, sleep, and wake up. Thread synchronization has a certain overhead for locking data nsoperation:– do not need to care about thread management, data synchronization, You can focus on the actions you need to perform –nsoperation is an object-oriented Gcd:–grand Central Dispatch is a multi-core programming solution developed by Apple. Ios4.0+ can be used, is an alternative to nsthread, Nsoperation's efficient and powerful technology –GCD is based on the C language<p><p></p></p><p><p><span style="font-size: 18px;"><strong>There are three types of queues in 3.GCD:</strong></span></p></p> <ol> <ol> <li>the <strong>main queue:</strong> the same as the main thread Function. In fact, the task that is submitted to the main queue executes in the main thread. The main queue can be obtained by calling Dispatch_get_main_queue (). Because the main queue is associated with the main thread, this is a serial Queue.</li> <li>globals <strong>queues:</strong> The global queue is a concurrent queue and is shared by the entire process. There are three global queues in the Process: high, medium (default), low three priority queues. You can call the Dispatch_get_global_queue function to pass in the priority level to access the Queue.</li> <li><strong>user queue:</strong> user queue (GCD does not call this queue, but does not have a specific name to describe the queue, so we call it a user Queue) is a <code>dispatch_queue_create</code> queue created with a Function. these queues are Serial. Because of this, they can be used to complete the synchronization mechanism, a bit like a mutex in a traditional thread.</li> </ol> </ol><p><p></p></p><p><p>Serial Queue synchronization: operation does not create new thread, operation sequence execution</p></p>Serial queue async: operations require a child thread, new threads, thread creation and recycling do not require programmer involvement, operation sequence execution, "safest choice"<p><p></p></p><p><p>Parallel queue synchronization: Operations do not create new threads, operation order execution</p></p><p><p>Parallel queue Async: The operation creates a new number of threads (with a number of tasks, executes on N threads), executes out of order, and, if there are other tasks before the queue, waits for the previous task to complete before executing;</p></p><p><p></p></p><p><p>Global Queue Async: Operations Create multiple threads, operate out of order, and, if there are other tasks before the queue, wait for the previous task to complete before executing. Equivalent to a serial Queue.</p></p><p><p>Global Queue synchronization: Operations do not create new threads, operation order execution</p></p><p><p>Primary queue async: operations should be executed sequentially on the main thread, not present</p></p><p><p></p></p><span style="font-size: 18pt;"><span style="font-size: 18pt;"><strong>4,. Results of nesting dispathch_sync (synchronous) tasks for different queues:</strong></span></span><p><p>1. Global queues, which are executed on the main thread, do not deadlock Dispatch_queue_priority_default</p></p><p><p>dispatch_queue_t q = dispatch_get_global_queue (dispatch_queue_priority_default, 0);</p></p><p><p>2. Parallel queues, which are executed on the main thread, do not deadlock dispatch_queue_concurrent</p></p><p><p>dispatch_queue_t q = dispatch_queue_create ("cn.itcast.gcddemo", dispatch_queue_concurrent);</p></p><p><p>3. Serial queue, deadlock, But the code before the nested synchronization operation is executed dispatch_queue_serial</p></p><p><p>dispatch_queue_t q = dispatch_queue_create ("cn.itcast.gcddemo", dispatch_queue_serial);</p></p><p><p>4. Home row, Direct deadlock Dispatch_get_main_queue ();</p></p><p><p>dispatch_queue_t q = Dispatch_get_main_queue ();</p></p><p><p>Dispatch_sync (q, ^{</p></p><p><p>NSLog (@ "sync task%@", [nsthread currentthread]);</p></p><p><p>Dispatch_sync (q, ^{</p></p><p><p>NSLog (@ "sync task%@", [nsthread currentthread]);</p></p><p><p>});</p></p><p><p>});</p></p><p id="c_a_2" class="c_a_2"><p id="c_a_2" class="c_a_2"></p></p><p><p>Multithreading Technology for iOS</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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service