在进行RTC应用开发时,会有加入频道,其它用户加入频道,房间的流发生变化等操作,对于这些行为,需要有相应的通知,发给界面层,需要进行异步处理,例如当有用户加入房间时,就触发对应的回调函数返回加入的结果,而这个结果是用户层被动获取的,因为不知道什么时候加入成功,通过常会有一个virtual方法让界面层override获取结果。 webrtc源码提供了线程异步调用的实现,代码在rtc_base/rtc_base, 如下所示:
在头文件async_invoker.h中有相关的说明: Invokes function objects (aka functors) asynchronously on a Thread, and owns the lifetime of calls (ie, when this object is destroyed, calls in flight are cancelled). AsyncInvoker can optionally execute a user-specified function when the asynchronous call is complete, or operates in fire-and-forget mode otherwise.
AsyncInvoker does not own the thread it calls functors on.
A note about async calls and object lifetimes: users should be mindful of object