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.

每當你與系統(tǒng)進行交互時王污,你必須會話費大量的時間為那個任務準備好蛉鹿。調(diào)用內(nèi)核或者涉及到上下文變化的系統(tǒng)其它其他的系統(tǒng)層和自己進程的調(diào)用相比灸姊,需要耗費大量的時間。因此布蔗,許多系統(tǒng)庫提供異步接口赢底,當你發(fā)送這個請求后,這個請求正在處理的過程中缚态,你可以繼續(xù)干其他事情扮碧。GCD就是這樣干的趟章,讓你可以可以在提交請求后,在回調(diào)中有結果芬萍。

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是一種基礎數(shù)據(jù)類型尤揣,用來匹配低水平系統(tǒng)層級的事件。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

數(shù)據(jù)可讀的時候

When it is possible to write data

數(shù)據(jù)可寫的時候

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

當進程發(fā)出fork或exec的時候

When a signal is delivered to the process

當信號被發(fā)送到進程的時候

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莽龟,你自己調(diào)用蠕嫁。

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取代了用于處理系統(tǒng)相關事件的回調(diào)函數(shù)。當你配置Dispatch sources的時候毯盈,你要指定你想要監(jiān)聽的事件和調(diào)度隊列剃毒,以及處理這些事件的代碼。你也可以使用block或者函數(shù)搂赋。當事件到達的時候赘阀,Dispatch sources會把block添加到隊列中進行執(zhí)行。

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會為你的程序提供連續(xù)的事件源基公。dispatch sources會保留在他的dispatch queue中直到顯示取消。當保留的時候宋欺,它會提交相關的任務代碼到隊列中轰豆,無論相關事件什么時候發(fā)生。一些事件齿诞,像定時器事件酸休,有規(guī)律發(fā)生但是大多數(shù)只在特定條件下發(fā)生。因此祷杈,dispatch sources會保留相關的度調(diào)度隊列斑司,以防止過早的釋放,當時間處于待處理的狀態(tài)下吠式。

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實現(xiàn)一個合并的計劃特占。如果一個新事件在其他事件出列和執(zhí)行之前到達糙置。dispatch source會把舊事件和新事件進行合并。根據(jù)情況是目,合并可能取代事件或更新信息谤饭。例如:signal-based dispatch source會提供最近的信息,并且報告有多少事件傳遞過來自從上次事件調(diào)用以來懊纳。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末揉抵,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子嗤疯,更是在濱河造成了極大的恐慌冤今,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件茂缚,死亡現(xiàn)場離奇詭異戏罢,居然都是意外死亡,警方通過查閱死者的電腦和手機脚囊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進店門龟糕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人悔耘,你說我怎么就攤上這事讲岁。” “怎么了衬以?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵缓艳,是天一觀的道長。 經(jīng)常有香客問我泄鹏,道長郎任,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任备籽,我火速辦了婚禮舶治,結果婚禮上,老公的妹妹穿的比我還像新娘车猬。我一直安慰自己霉猛,他們只是感情好,可當我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布珠闰。 她就那樣靜靜地躺著惜浅,像睡著了一般。 火紅的嫁衣襯著肌膚如雪伏嗜。 梳的紋絲不亂的頭發(fā)上坛悉,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天伐厌,我揣著相機與錄音,去河邊找鬼裸影。 笑死挣轨,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的轩猩。 我是一名探鬼主播卷扮,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼均践!你這毒婦竟也來了晤锹?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤彤委,失蹤者是張志新(化名)和其女友劉穎鞭铆,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體葫慎,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡衔彻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了偷办。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片艰额。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖椒涯,靈堂內(nèi)的尸體忽然破棺而出柄沮,到底是詐尸還是另有隱情,我是刑警寧澤废岂,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布祖搓,位于F島的核電站,受9級特大地震影響湖苞,放射性物質(zhì)發(fā)生泄漏拯欧。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一财骨、第九天 我趴在偏房一處隱蔽的房頂上張望镐作。 院中可真熱鬧,春花似錦隆箩、人聲如沸该贾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽杨蛋。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間逞力,已是汗流浹背曙寡。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留寇荧,地道東北人卵皂。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像砚亭,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子殴玛,可洞房花燭夜當晚...
    茶點故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,497評論 0 23
  • 春天捅膘,藍藍的天空和明媚的陽光 再加之美麗的花朵簡直是再不能合適的拍攝季節(jié) 作為一個拍攝小白,在不斷的實踐和總結中得...
    白眉小耳閱讀 349評論 6 3
  • 我從一開始滚粟,就不太喜歡自己現(xiàn)在的工作……促銷寻仗、發(fā)貨、客服凡壤、物流署尤,balabala…一堆無聊的工作⊙窍溃“微店”曹体,聽起來...
    囚徒_9527閱讀 140評論 0 0
  • 幾年前在雜志上看到的一幅插畫至今在我腦海中揮之不去:三個人因腳底的書疊起來的高度不同,而看到不一樣的景色硝烂,第一個人...
    奈奈留閱讀 427評論 0 0
  • 花開林間密箕别,亭立云水低。此去關山遠滞谢,他鄉(xiāng)無歸期串稀。
    文瀾海閣閱讀 209評論 1 1