webrtc源码学习 - 3种异步处理

1、webrtc 3中异步调用方式

在webrtc 任务中有3种不同的异步处理方式,用于解决不同的问题,如下
1)taskqueue 每个队列上有一个线程执行,线程不需要指定
2)asyncTask 在指定的线程上执行,需要指定线程
3)SynchronousMethodCall 在指定线程执行完成后,同步等待结果(跨线程同步调用)

2、具体实现

2.1 TaskQueue

在这里插入图片描述

TaskQueue :是TaskQueueBase 的代理封装,是依赖注入的方法实现。
TaskQueueBase : 抽象接口
TaskQueueWin:
1、创建一个线程,并启动,每个任务队列有一个线程
2、在线程不断的读取task, 并执行
3、可以支持延迟任务

// 创建线程
TaskQueueWin::TaskQueueWin(absl::string_view queue_name,
                           rtc::ThreadPriority priority)
    : thread_(&TaskQueueWin::ThreadMain, this, queue_name, priority),
      in_queue_(::CreateEvent(nullptr, true, false, nullptr)) {
   
  RTC_DCHECK(in_queue_);
  thread_.Start();
  
  //启动线程
  rtc::Event event(false, false);
  RTC_CHECK(thread_.QueueAPC(&InitializeQueueThread,
                             reinterpret_cast<ULONG_PTR>(&event)));
  //等待线程初始化完成                           
  event.Wait(rtc::Event::kForever);
}
  
  //线程主循环
void TaskQueueWin::RunThreadMain() {
   
  CurrentTaskQueueSetter set_current(this);
  HANDLE handles[2] = {
   *timer_.event_for_wait(), in_queue_};
  while (true) {
   
    // Make sure we do an alertable wait as that's required to allow APCs to run
    // (e.g. required for InitializeQueueThread and stopping the thread in
    // PlatformThread).
    DWORD result = ::MsgWaitForMultipleObjectsEx(
        arraysize(handles), handles, INFINITE, QS_ALLEVENTS, MWMO_ALERTABLE);
    RTC_CHECK_NE(WAIT_FAILED, result);
    if (result == (WAIT_OBJECT_0 + 2)) {
   
      // There are messages in the message queue that need to be handled.
      // PeekMessage 
      if (!ProcessQueuedMessages())
        break;
    }

    if (result == WAIT_OBJECT_0 ||
        (!timer_tasks_
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值