Run Loop筆記

The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none.

A run loop receives events from two different types of sources. Input sources deliver asynchronous events, usually messages from another thread or from a different application. Timer sources deliver synchronous events, occurring at a scheduled time or repeating interval. Both types of source use an application-specific handler routine to process the event when it arrives.

In addition to handling sources of input, run loops also generate notifications about the run loop’s behavior. Registered run-loop observers can receive these notifications and use them to do additional processing on the thread. You use Core Foundation to install run-loop observers on your threads.

Listing 3-1 Creating a run loop observer

- (void)threadMain
{
    // The application uses garbage collection, so no autorelease pool is needed.
    NSRunLoop* myRunLoop = [NSRunLoop currentRunLoop];
 
    // Create a run loop observer and attach it to the run loop.
    CFRunLoopObserverContext  context = {0, self, NULL, NULL, NULL};
    CFRunLoopObserverRef    observer = CFRunLoopObserverCreate(kCFAllocatorDefault,
            kCFRunLoopAllActivities, YES, 0, &myRunLoopObserver, &context);
 
    if (observer)
    {
        CFRunLoopRef    cfLoop = [myRunLoop getCFRunLoop];
        CFRunLoopAddObserver(cfLoop, observer, kCFRunLoopDefaultMode);
    }
 
    // Create and schedule the timer.
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
                selector:@selector(doFireTimer:) userInfo:nil repeats:YES];
 
    NSInteger    loopCount = 10;
    do
    {
        // Run the run loop 10 times to let the timer fire.
        [myRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
        loopCount--;
    }
    while (loopCount);
}

Starting the Run Loop

Starting the run loop is necessary only for the secondary threads in your application. A run loop must have at least one input source or timer to monitor. If one is not attached, the run loop exits immediately.

There are several ways to start the run loop, including the following:

  • Unconditionally
  • With a set time limit
  • In a particular mode

CFRunLoop Reference

There is exactly one run loop per thread. You neither create nor destroy a thread’s run loop. Core Foundation automatically creates it for you as needed. You obtain the current thread’s run loop with CFRunLoopGetCurrent. Call CFRunLoopRun to run the current thread’s run loop in the default mode until the run loop is stopped with CFRunLoopStop. You can also call CFRunLoopRunInMode to run the current thread’s run loop in a specified mode for a set period of time (or until the run loop is stopped). A run loop can only run if the requested mode has at least one source or timer to monitor.

參考:
1.https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html
2.https://developer.apple.com/library/ios/documentation/CoreFoundation/Reference/CFRunLoopRef/index.html#//apple_ref/doc/uid/20001441
3.https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/index.html#//apple_ref/doc/uid/TP40003725
4.https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-131281
5.https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSPort_Class/index.html#//apple_ref/occ/cl/NSPort

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末掸鹅,一起剝皮案震驚了整個濱河市同衣,隨后出現(xiàn)的幾起案子懂算,更是在濱河造成了極大的恐慌侠坎,老刑警劉巖本涕,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件舶吗,死亡現(xiàn)場離奇詭異赃份,居然都是意外死亡,警方通過查閱死者的電腦和手機铜秆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門淹真,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人连茧,你說我怎么就攤上這事核蘸∥∨矗” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵客扎,是天一觀的道長祟峦。 經(jīng)常有香客問我,道長徙鱼,這世上最難降的妖魔是什么宅楞? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮袱吆,結(jié)果婚禮上厌衙,老公的妹妹穿的比我還像新娘。我一直安慰自己绞绒,他們只是感情好迅箩,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著处铛,像睡著了一般。 火紅的嫁衣襯著肌膚如雪拐揭。 梳的紋絲不亂的頭發(fā)上撤蟆,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機與錄音堂污,去河邊找鬼家肯。 笑死,一個胖子當著我的面吹牛盟猖,可吹牛的內(nèi)容都是我干的讨衣。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼式镐,長吁一口氣:“原來是場噩夢啊……” “哼反镇!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起娘汞,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤歹茶,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后你弦,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體惊豺,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年禽作,在試婚紗的時候發(fā)現(xiàn)自己被綠了尸昧。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡旷偿,死狀恐怖烹俗,靈堂內(nèi)的尸體忽然破棺而出爆侣,到底是詐尸還是另有隱情,我是刑警寧澤衷蜓,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布累提,位于F島的核電站,受9級特大地震影響磁浇,放射性物質(zhì)發(fā)生泄漏斋陪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一置吓、第九天 我趴在偏房一處隱蔽的房頂上張望无虚。 院中可真熱鬧,春花似錦衍锚、人聲如沸友题。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽度宦。三九已至,卻和暖如春告匠,著一層夾襖步出監(jiān)牢的瞬間戈抄,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工后专, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留划鸽,地道東北人。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓戚哎,卻偏偏與公主長得像裸诽,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子型凳,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

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