Dispatch Sources

Whenever you interact with the underlying system, you must be prepared for that task to take a nontrivial amount of time. Calling down to the kernel or other system layers involves a change in context that is reasonably expensive compared to calls that occur within your own process. As a result, many system libraries provide asynchronous interfaces to allow your code to submit a request to the system and continue to do other work while that request is processed. Grand Central Dispatch builds on this general behavior by allowing you to submit your request and have the results reported back to your code using blocks and dispatch queues.

每當你與系統進行交互時,你必須會話費大量的時間為那個任務準備好。調用內核或者涉及到上下文變化的系統其它其他的系統層和自己進程的調用相比,需要耗費大量的時間。因此,許多系統庫提供異步接口,當你發送這個請求后,這個請求正在處理的過程中,你可以繼續干其他事情。GCD就是這樣干的,讓你可以可以在提交請求后,在回調中有結果。

About Dispatch Sources

關于Dispatch Sources

A?dispatch source?is a fundamental data type that coordinates the processing of specific low-level system events. Grand Central Dispatch supports the following types of dispatch sources:

Dispatch Sources是一種基礎數據類型,用來匹配低水平系統層級的事件。GCD支持下邊這幾種Dispatch Sources。

Timer dispatch sources?generate periodic notifications.

定時器會生成定期的通知。

Signal dispatch sources?notify you when a UNIX signal arrives.

Signal dispatch sources會通知你,在Unix信號到達的時候。

Descriptor sources?notify you of various file- and socket-based operations, such as:

Descriptor sources會在文件操作的時候通知你:

When data is available for reading

數據可讀的時候

When it is possible to write data

數據可寫的時候

When files are deleted, moved, or renamed in the file system

文件被刪除。移動,重命名的時候

When file meta information changes

文件原信息改變的時候

Process dispatch sources?notify you of process-related events, such as:

Process dispatch sources會通知你當事件變化的時候

When a process exits

進程終止的時候

When a process issues a fork or exec type of call

當進程發出fork或exec的時候

When a signal is delivered to the process

當信號被發送到進程的時候

Mach port dispatch sources?notify you of Mach-related events.

Mach port dispatch sources會通知你mach相關事件

Custom dispatch sources?are ones you define and trigger yourself.

自定義事件是你自定義的source,你自己調用。

Dispatch sources replace the asynchronous callback functions that are typically used to process system-related events. When you configure a dispatch source, you specify the events you want to monitor and the dispatch queue and code to use to process those events. You can specify your code using?block objects?or functions. When an event of interest arrives, the dispatch source submits your block or function to the specified dispatch queue for execution.

Dispatch sources取代了用于處理系統相關事件的回調函數。當你配置Dispatch sources的時候,你要指定你想要監聽的事件和調度隊列,以及處理這些事件的代碼。你也可以使用block或者函數。當事件到達的時候,Dispatch sources會把block添加到隊列中進行執行。

Unlike tasks that you submit to a queue manually, dispatch sources provide a continuous source of events for your application. A dispatch source remains attached to its dispatch queue until you cancel it explicitly. While attached, it submits its associated task code to the dispatch queue whenever the corresponding event occurs. Some events, such as timer events, occur at regular intervals but most occur only sporadically as specific conditions arise. For this reason, dispatch sources retain their associated dispatch queue to prevent it from being released prematurely while events may still be pending.

和你手動添加到隊列中的任務不一樣,dispatch sources會為你的程序提供連續的事件源。dispatch sources會保留在他的dispatch queue中直到顯示取消。當保留的時候,它會提交相關的任務代碼到隊列中,無論相關事件什么時候發生。一些事件,像定時器事件,有規律發生但是大多數只在特定條件下發生。因此,dispatch sources會保留相關的度調度隊列,以防止過早的釋放,當時間處于待處理的狀態下。

To prevent events from becoming backlogged in a dispatch queue, dispatch sources implement an event coalescing scheme. If a new event arrives before the event handler for a previous event has been dequeued and executed, the dispatch source coalesces the data from the new event data with data from the old event. Depending on the type of event, coalescing may replace the old event or update the information it holds. For example, a signal-based dispatch source provides information about only the most recent signal but also reports how many total signals have been delivered since the last z of the event handler.

在隊列中,為了防止事件積壓,dispatch sources實現一個合并的計劃。如果一個新事件在其他事件出列和執行之前到達。dispatch source會把舊事件和新事件進行合并。根據情況,合并可能取代事件或更新信息。例如:signal-based dispatch source會提供最近的信息,并且報告有多少事件傳遞過來自從上次事件調用以來。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,461評論 6 532
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,538評論 3 417
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,423評論 0 375
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,991評論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,761評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,207評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,268評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,419評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,959評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,782評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,983評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,528評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,222評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,653評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,901評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,678評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,978評論 2 374

推薦閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,708評論 0 23
  • 春天,藍藍的天空和明媚的陽光 再加之美麗的花朵簡直是再不能合適的拍攝季節 作為一個拍攝小白,在不斷的實踐和總結中得...
    白眉小耳閱讀 360評論 6 3
  • 我從一開始,就不太喜歡自己現在的工作……促銷、發貨、客服、物流,balabala…一堆無聊的工作。“微店”,聽起來...
    囚徒_9527閱讀 144評論 0 0
  • 幾年前在雜志上看到的一幅插畫至今在我腦海中揮之不去:三個人因腳底的書疊起來的高度不同,而看到不一樣的景色,第一個人...
    奈奈留閱讀 429評論 0 0
  • 花開林間密,亭立云水低。此去關山遠,他鄉無歸期。
    文瀾海閣閱讀 214評論 1 1