NSThread创建CFRunLoop

本文介绍了CFRunLoop的基本原理和作用,它负责监控线程的输入源,如用户输入、网络连接等。CFRunLoop管理三种类型的对象:sources、timers和observers。对象必须与特定的run loop模式关联,以在适当的时候接收回调。文章还提到,每个线程有一个唯一的run loop,并可通过CFRunLoopGetCurrent获取。Cocoa应用基于CFRunLoop构建自己的事件循环,开发者可以通过NSRunLoop的getCFRunLoop方法获取对应CFRunLoopRef。
摘要由CSDN通过智能技术生成

首先是文档对CFRunLoop部分的一个概述,可以帮助我们简单的了解一下CFRunLoop的主要的特性:

Overview

A CFRunLoop object monitors sources of input to a task and dispatches control when they become ready for processing. Examples of input sources might include user input devices, network connections, periodic or time-delayed events, and asynchronous callbacks.

Three types of objects can be monitored by a run loop: sources (CFRunLoopSource Reference), timers (CFRunLoopTimer Reference), and observers (CFRunLoopObserver Reference). To receive callbacks when these objects need processing, you must first place these objects into a run loop withCFRunLoopAddSource,CFRunLoopAddTimer, orCFRunLoopAddObserver. You can later remove an object from the run loop (or invalidate it) to stop receiving its callback.

Each source, timer, and observer added to a run loop must be associated with one or more run loop modes. Modes determine what events are processed by the run loop during a given iteration. Each time the run loop executes, it does so in a specific mode. While in that mode, the run loop processes only the events associated with sources, timers, and observers associated with that mode. You assign most sources to the default run loop mode (designated by thekCFRunLoopDefaultMode constant), which is used to process events when the application (or thread) is idle. However, the system defines other modes and may execute the run loop in those other modes to limit which sources, timers, and observers are processed. Because run-loop modes are simply specified as strings, you can also define your own custom modes to li

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值