webrtc thread introduce

webrtc thread introduce

1,webrtc thread常用到的类有MessageHandler,Thread。
2,MessageHandler用来作为线程处理函数的回调,实现如下:

class MessageHandler {
 public:
  virtual void OnMessage(Message* msg) = 0;

 protected:
  MessageHandler() {}
  virtual ~MessageHandler();

 private:
  DISALLOW_COPY_AND_ASSIGN(MessageHandler); //禁掉copy和赋值方法
};

3,假设类A要使用webrtc的thread,那么使用方式如下:

class A:public MessageHandler{
public:
  A():mThread(Null){
    mThread = new(std::nothow) talk_base::Thread();
    if(mThread){
      mThread->start();
    }
  };
  ~A(){
    if(mThread){
      mThread->stop();
      delete mThread;
      mThread = Null;
    }
  }
  void Test(string&test){
      mThread->post(this,MSG_THREAD_TEST,new MessageData(test));
  }
private:
enum {
      MSG_THREAD_TEST
   };
  void OnMessage(talk_base::Message* msg){
      switch(msg->message_id){
         case MSG_THREAD_TEST:{
            MessageData * obs = static_cast<MessageData*>(msg->pdata);
            //TODO 
            OnTest(obs->name);
            delete obs;
         }
           break;
         default:
           break;
      }
  }
  // 在thread中执行
  void OnTest(string&test){

  }

  Thread* mThread;

}

//direction for use

A a;
a.Test("test");

4,thread中方法说明:

  • start(),线程启动;
  • restart(),重启线程;
  • stop(),线程停止;
  • post(),异步执行指定的事件;
  • send(),同步执行指定的事件;
  • PostDelayed(),异步延时执行指定的事件;
  • clear(),清空队列中的消息;
    有一点需要注意stop()后不能再次start(),需要调用restart();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值