Dispatch Queues 线程池

Dispatch Queues

Dispatch queues are a C-based mechanism for executing custom tasks. A dispatch queue executes tasks either serially or concurrently but always in a first-in, first-out order. (In other words, a dispatch queue always dequeues and starts tasks in the same order in which they were added to the queue.) A serial dispatch queue runs only one task at a time, waiting until that task is complete before dequeuing and starting a new one. By contrast, a concurrent dispatch queue starts as many tasks as it can without waiting for already started tasks to finish. 

Dispatch queues have other benefits: 

  • They provide a straightforward and simple programming interface.

  • They offer automatic and holistic thread pool management.

  • They provide the speed of tuned assembly. 

  • They are much more memory efficient (because thread stacks do not linger in application memory).

  • They do not trap to the kernel under load.

  • The asynchronous dispatching of tasks to a dispatch queue cannot deadlock the queue.

  • They scale gracefully under contention.

  • Serial dispatch queues offer a more efficient alternative to locks and other synchronization primitives.

The tasks you submit to a dispatch queue must be encapsulated inside either a function or a block object. Block objects are a C language feature introduced in OS X v10.6 and iOS 4.0 that are similar to function pointers conceptually, but have some additional benefits. Instead of defining blocks in their own lexical scope, you typically define blocks inside another function or method so that they can access other variables from that function or method. Blocks can also be moved out of their original scope and copied onto the heap, which is what happens when you submit them to a dispatch queue. All of these semantics make it possible to implement very dynamic tasks with relatively little code. 

Dispatch queues are part of the Grand Central Dispatch technology and are part of the C runtime. For more information about using dispatch queues in your applications, see Dispatch Queues. For more information about blocks and their benefits, see Blocks Programming Topics

 

https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/ConcurrencyandApplicationDesign/ConcurrencyandApplicationDesign.html#//apple_ref/doc/uid/TP40008091-CH100-SW2

 

转载于:https://www.cnblogs.com/feng9exe/p/8024611.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
useDispatch是react-redux库中的一个Hook,用于在函数组件中获取dispatch函数。它可以用来触发action并将其发送到Redux store中。通过useDispatch,我们可以在组件中使用dispatch函数来更新状态或执行其他操作。在使用react-redux时,我们可以通过以下方式来使用useDispatch: ```javascript import { useDispatch } from 'react-redux'; const MyComponent = () => { const dispatch = useDispatch(); const handleClick = () => { dispatch({ type: 'INCREMENT' }); }; return ( <button onClick={handleClick}>Increment</button> ); }; ``` 在上面的例子中,我们使用useDispatch来获取dispatch函数,并在按钮的点击事件中使用dispatch来触发一个名为'INCREMENT'的action。这样就可以通过dispatch函数来更新Redux store中的状态了。 #### 引用[.reference_title] - *1* *2* [深刻理解useSelector,useDispatch以及useReducer](https://blog.csdn.net/qq_43372697/article/details/126968428)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [如何使用useDispatch useSelector对我们react中的代码进行简化。](https://blog.csdn.net/m0_70718568/article/details/127571026)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值