GCD

本文深入探讨iOS开发中并发与队列的应用,详细介绍了如何创建与使用全局并发队列、创建与管理序列队列,以及如何从当前线程异步加入globalqueue中,确保任务高效执行。


只是个队列,不是一个线程。

Getting the Global Concurrent Dispatch Queues


The system provides each application with three concurrent dispatch queues. These queues are global to the application and are differentiated only by their priority level. Because they are global, you do not create them explicitly. Instead, you ask for one of the queues using the dispatch_get_global_queue function, as shown in the following example:

dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

Creating Serial Dispatch Queues

Unlike concurrent queues, which are created for you, you must explicitly create and manage any serial queues you want to use. You can create any number of serial queues for your application but should avoid creating large numbers of serial queues solely as a means to execute as many tasks simultaneously as you can. If you want to execute large numbers of tasks concurrently, submit them to one of the global concurrent queues. When creating serial queues, try to identify a purpose for each queue, such as protecting a resource or synchronizing some key behavior of your application.

Listing 3-2 shows the steps required to create a custom serial queue. The dispatch_queue_create function takes two parameters: the queue name and a set of queue attributes. The debugger and performance tools display the queue name to help you track how your tasks are being executed. The queue attributes are reserved for future use and should be NULL.

Listing 3-2  Creating a new serial queue

dispatch_queue_t queue;
queue = dispatch_queue_create("com.example.MyQueue", NULL);

Getting Common Queues at Runtime

Grand Central Dispatch provides functions to let you access several common dispatch queues from your application:

  • Use the dispatch_get_current_queue function for debugging purposes or to test the identity of the current queue. Calling this function from inside a block object returns the queue to which the block was submitted (and on which it is now presumably running). Calling this function from outside of a block returns the default concurrent queue for your application.

  • Use the dispatch_get_main_queue function to get the serial dispatch queue associated with your application’s main thread. This queue is created automatically for Cocoa applications and for applications that either call the dispatch_main function or configure a run loop (using either the CFRunLoopRef type or anNSRunLoop object) on the main thread.

  • Use the dispatch_get_global_queue function to get any of the shared concurrent queues. 

Adding a Single Task to a Queue

There are two ways to add a task to a queue: asynchronously or synchronously. When possible, asynchronous execution using the dispatch_async and dispatch_async_f functions is preferred over the synchronous alternative. 

adding blocks or functions asynchronously lets you schedule the execution of the code and continue to do other work from the calling thread. 
从当前线程异步加入 global queue中
  1. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
  2.     // 耗时的操作  
  3. // 从当前线程异步到主线程queue中执行
  4.     dispatch_async(dispatch_get_main_queue(), ^{  
  5.         // 更新界面  
  6.     });  
  7. });






在 Python 中,计算两个数的最大公约数(GCD)有多种方法,主要包括以下几种实现方式: ### 1. 使用内置库 `math` 中的 `gcd` 函数 Python 3.5 及以上版本的 `math` 模块提供了 `gcd` 函数,可以直接用于计算两个整数的最大公约数。该方法简洁高效,推荐在支持的环境中使用。 ```python import math a = 24 b = 45 print(math.gcd(a, b)) # 输出 3 ``` ### 2. 使用辗转相除法(欧几里得算法) 这是经典的数学算法,通过反复取余直到余数为零来求解最大公约数。其核心逻辑是将较大的数除以较小的数,并用较小的数和余数继续操作,直到余数为零,此时的除数即为最大公约数[^1]。 ```python def gcd(x, y): while y != 0: x, y = y, x % y return x print(gcd(24, 45)) # 输出 3 ``` ### 3. 使用更相减损法(辗转相减法) 这种方法通过不断用较大的数减去较小的数,直到两个数相等为止。该方法在没有取余操作的早期计算机中较为常用[^3]。 ```python def gcd_subtract(a, b): while a != b: if a > b: a -= b else: b -= a return a print(gcd_subtract(24, 45)) # 输出 3 ``` ### 4. 使用递归实现辗转相除法 递归方式可以更简洁地表达算法逻辑,适合理解函数式编程思想。 ```python def gcd_recursive(x, y): if y == 0: return x else: return gcd_recursive(y, x % y) print(gcd_recursive(24, 45)) # 输出 3 ``` ### 5. 使用 `fractions` 模块中的 `gcd` 函数(适用于 Python 3.5 之前版本) 在较早版本的 Python 中,可以使用 `fractions` 模块中的 `gcd` 函数,但该方法已在 Python 3.5 被 `math.gcd` 替代。 ```python from fractions import gcd print(gcd(24, 45)) # 输出 3 ``` ### 注意事项 - `math.gcd` 返回的结果始终为非负整数,即使输入包含负数。 - 如果输入的两个数均为零,则 `gcd` 无定义,但在某些实现中可能返回 `0`。 - 若需计算最小公倍数(LCM),可通过公式 `lcm(a, b) = abs(a * b) // gcd(a, b)` 实现[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值