<span id="Label3"></p><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">iOS multi-thread gcd small note</span></span><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">Introduction to the iOS multithreading scenario</span></span><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">It is understood from various sources that there are currently 4 multi-threaded scenarios in ios, namely the following 4:</span></span><br><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;"><strong style="font-weight: bold;">1, Pthreads</strong></span></span><br><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">This is a multi-threaded API that can be used on many operating systems, and is based on the <strong style="font-weight: bold;">C language</strong> , which needs to be included in OC</span></span><br><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;"> <span style="color: blue;">#import</span><span style="color: red;"><pthread.h></span> </span></span><br><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">using this multithreaded scenario requires manual processing of the transitions of each state of the thread, that is, managing the Thread's life Cycle. </span></span><br><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;"><strong style="font-weight: bold;">2, Nsthread</strong></span></span><br><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">This multithreaded scheme is packaged by Apple and is an object-oriented solution, so you can manipulate thread objects directly, relatively conveniently, and have their lifecycle manually managed</span> .</span><br><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;"><strong style="font-weight: bold;">3, GCD</strong></span></span><br><span style="font-family: ‘Microsoft YaHei‘;">The full <span style="font-family: ‘Microsoft YaHei‘;">name of Grand Central Dispatch is a multithreaded solution developed by Apple. </span></span><br><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;"><strong style="font-weight: bold;">4, nsoperation & Nsoperationqueue</strong></span></span><br><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">Nsoperation is Apple's further encapsulation of gcd, fully object-oriented</span></span><br><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">well, after a general introduction of these multi-threaded solutions, we formally chose to enter the GCD. </span></span></p><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">GCD</span></span><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><p style="margin-top:0; margin-right:0; margin-bottom:10px; margin-left:0; line-height:1.8em Style = " font-family: ' microsoft yahei ';">GCD provides a whole new set of API common users that can split the work that an application needs to perform into smaller chunks that can be dispersed across multiple threads and multiple Cpus. It can automatically manage the thread life cycle (thread creation, task scheduling, thread destruction). The user only needs to tell GCD the task they are going to perform. GCD is also using <span style="color:red;" \ c language < span>. It can be accessed using the program library (Block). </span> <br> <span style="font-family: ' Microsoft Yahei ';">GCD has two concepts: task and queue </span> <br> <span style="font-family: ' Microsoft Yahei '; " the task is action, which what the user wants to do, and can be represented by a piece of code. task execution divided into synchronous asynchronous. < span> <br> <span style="font-family: ' Microsoft Yahei ';"> Queues are used to hold tasks, and queues are divided into serial and parallel queues. </span> <br> <span style="font-family: ' Microsoft yahei ';"><strong style="font-weight:bold;"> create queue </strong> </span> <br> <span style="font-family: ' Microsoft Yahei ';" in all task queues, there is a home column that responsible for refreshing the ui, and any time-consuming tasks should not be placed main queue. the code creating home as follows: < span> </span></span></p></p><pre><pre><span style="font-family: ‘Microsoft YaHei‘;"><code class="hljs objectivec" style="background-color: #d6dbdf; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-built_in" style="color: #f99b15;">dispatch_queue_t</span> queue = ispatch_get_main_queue();</code></span></pre></pre><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><span style="font-family: ‘Microsoft YaHei‘;"><strong style="font-weight: bold;">Create a serial queue</strong></span></p></p><pre><pre> <span style="font-family: ' Microsoft yahei ';"><code class="hljs objectivec" style="background-color: #d6dbdf; border:0; border-radius:4px; Color: #4f424c; font-size:90%; display:block; Overflow-x: auto; Background: #e7e9db; -webkit-text-size-adjust:none; padding:0.5em; "> <span class="hljs-built_in" style="color: #f99b15;">dispatch_queue_t </span> queue = Dispatch_queue_create (<span class="hljs-string" style="color: #48b685;"> "testQueue1" </span>, dispatch_queue_serial); <span class="hljs-built_in" style="color: #f99b15;">dispatch_queue_t </span> queue = Dispatch_queue_create (<span class="hljs-string" style="color: #48b685;"> "testQueue2" </span>, <span class="hljs-literal" style="color: #f99b15;">null </span>); </code> </span> </pre></pre><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><span style="font-family: ‘Microsoft YaHei‘;">The first parameter is the identifier of the queue, which is used to uniquely identify the Queue. The second parameter indicates whether the Queue's nature is serial or parallel.</span><br><span style="font-family: ‘Microsoft YaHei‘;"><strong style="font-weight: bold;">Create a parallel queue</strong></span></p></p><pre><pre><span style="font-family: ‘Microsoft YaHei‘;"><code class="hljs objectivec" style="background-color: #d6dbdf; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"> <span class="hljs-built_in" style="color: #f99b15;">dispatch_queue_t</span> queue = dispatch_queue_create(<span class="hljs-string" style="color: #48b685;">"testQueue3"</span>, DISPATCH_QUEUE_CONCURRENT); </code></span></pre></pre><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><span style="font-family: ‘Microsoft YaHei‘;"><strong style="font-weight: bold;">Global parallel queue</strong></span><br><span style="font-family: ‘Microsoft YaHei‘;">This queue is provided by the system and is obtained in the following way</span></p></p><pre><pre><span style="font-family: ‘Microsoft YaHei‘;"><code class="hljs lisp" style="background-color: #d6dbdf; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;">dispatch_queue_t queue = dispatch_get_global_queue<span class="hljs-list">(<span class="hljs-keyword" style="color: #815ba4;">DISPATCH_QUEUE_PRIORITY_DEFAULT</span>, <span class="hljs-number" style="color: #f99b15;">0</span>)</span><span class="hljs-comment" style="color: #776e71;">;</span></code></span></pre></pre><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><span style="font-family: ‘Microsoft YaHei‘;">The first parameter indicates the priority of the queue, the second parameter is temporarily unused, and is set to 0</span></p></p><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><span style="font-family: ‘Microsoft YaHei‘;"><strong style="font-weight: bold;">Create a task</strong></span><br><span style="font-family: ‘Microsoft YaHei‘;">Tasks are divided into synchronous tasks and asynchronous tasks</span><br><span style="font-family: ‘Microsoft YaHei‘;"><strong style="font-weight: bold;">Create a synchronization task</strong></span></p></p><pre><pre><span style="font-family: ‘Microsoft YaHei‘;"><code class="hljs less" style="background-color: #d6dbdf; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"> <span class="hljs-tag" style="color: #ef6155;">dispatch_sync</span>(<testQueue3>, ^{ <span class="hljs-comment" style="color: #776e71;">//程序块,即要执行的具体任务</span> <span class="hljs-comment" style="color: #776e71;">//GCD获取这里的整个程序块,然后将它传递给一个后台线程,</span> <span class="hljs-comment" style="color: #776e71;">//程序块将在这里一次执行异步,就像在主线中一样</span> });</code></span></pre></pre><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><span style="font-family: ‘Microsoft YaHei‘;">The first parameter passes in the queue name, which is the queue to which the newly created task is added, and the second parameter is a block</span><br><span style="font-family: ‘Microsoft YaHei‘;"><strong style="font-weight: bold;">To create an asynchronous task</strong></span></p></p><pre><pre><span style="font-family: ‘Microsoft YaHei‘;"><code class="hljs less" style="background-color: #d6dbdf; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"> <span class="hljs-tag" style="color: #ef6155;">dispatch_async</span>(<testQueue3>, ^{ <span class="hljs-comment" style="color: #776e71;">//程序块,即要执行的任务</span> });</code></span></pre></pre><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><span style="font-family: ‘Microsoft YaHei‘;"><strong style="font-weight: bold;">Returning the main thread from other threads</strong></span></p></p><pre><pre><span style="font-family: ‘Microsoft YaHei‘;"><code class="hljs lisp" style="background-color: #d6dbdf; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;">dispatch_async<span class="hljs-list">(<span class="hljs-keyword" style="color: #815ba4;">dispatch_get_main_queue</span><span class="hljs-list">()</span>, ^{})</span><span class="hljs-comment" style="color: #776e71;">;</span></code></span></pre></pre><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">Summary</span></span><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><span style="font-family: ‘Microsoft YaHei‘;">In fact, GCD is helping you encapsulate the multi-threaded Operation.</span><br><span style="font-family: ‘Microsoft YaHei‘;">A queue is equivalent to a thread, and the tasks in the queue are executed in FIFO order.</span><br><span style="font-family: ‘Microsoft YaHei‘;">Temporarily only so superficial understanding, time is sufficient, still want to actually write code, this can understand more deeply.</span></p></p><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><p style="margin-top: 0; margin-right: 0; margin-bottom: 10px; margin-left: 0; line-height: 1.8em;"><span style="font-family: ‘Microsoft YaHei‘;">Section Code reference: HTTP://WWW.JIANSHU.COM/P/0B0D9B1F1F19</span></p></p><p><p>iOS multi-thread GCD Small note</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