WebRTC学习之函数的异步执行

来源:互联网 发布:windows select epoll 编辑:程序博客网 时间:2024/06/05 14:12

WebRTC在asyncinvoker.h和asyncivoker.cpp中实现了函数的异步执行。asyncinvoker.h的注释中给出了一个小例子,本文的学习就是从这个例子开始的。但是这个例子和单元测试代码都只演示了如何让一个函数在子线程中执行,如下所示。

myclass.h

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "webrtc/base/asyncinvoker.h"  
  2. #include <iostream>  
  3. #include <memory>  
  4.   
  5. class MyClass   
  6. {  
  7.   
  8. public:  
  9.     MyClass();  
  10.     void OneTask(rtc::Thread *threadint x);  
  11.   
  12.     void AnotherAsyncTask(int x);  
  13.       
  14. private:  
  15.     std::unique_ptr<rtc::AsyncInvoker> invoker;  
  16. };  
myclass.cpp
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "myclass.h"  
  2. MyClass::MyClass()  
  3. {  
  4.     invoker.reset(new rtc::AsyncInvoker());  
  5.     std::cout << "Main Thread ID:" << rtc::Thread::Current()->GetId() << std::endl;  
  6. }  
  7.   
  8. void MyClass::OneTask(rtc::Thread *threadint x)  
  9. {  
  10.     invoker->AsyncInvoke<void>(RTC_FROM_HERE, thread, rtc::Bind(&MyClass::AnotherAsyncTask, (MyClass*)this, x));  
  11. }  
  12. void MyClass::AnotherAsyncTask(int x)  
  13. {  
  14.     std::cout << "Worker Thread ID:" << rtc::Thread::Current()->GetId() << std::endl;  
  15.     std::cout << "Input Value Is:" << x << std::endl;  
  16. }  
main.cpp

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "myclass.h"  
  2. int main()  
  3. {  
  4.     std::unique_ptr<rtc::Thread> myThread = rtc::Thread::Create();  
  5.     myThread->Start();  
  6.     MyClass *myClass = new MyClass;  
  7.     myClass->OneTask(myThread.get(), 10);  
  8.   
  9.     Sleep(10000);  
  10.   
  11.     return 0;  
  12. }  

打印结果

从上图输出的线程ID可以看出,函数AnotherAsyncTask是在子线程中执行的。

但是如何在子线程中调用一个函数,并让该函数中主线程中执行呢,可以将上述代码稍作更改,如下所示。

myclass.h

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "webrtc/base/asyncinvoker.h"  
  2. #include <iostream>  
  3. #include <memory>  
  4.   
  5. class MyClass   
  6. {  
  7.   
  8. public:  
  9.     MyClass();  
  10.     void OneTask(rtc::Thread *threadint x);  
  11.   
  12.     void AnotherAsyncTask(int x);  
  13.       
  14.     void myFunction(int x);  
  15.       
  16. private:  
  17.     std::unique_ptr<rtc::AsyncInvoker> invoker;  
  18.     rtc::Thread *mainThread;  
  19. };  
myclass.cpp

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "myclass.h"  
  2. MyClass::MyClass()  
  3. {  
  4.     invoker.reset(new rtc::AsyncInvoker());  
  5.     std::cout << "Main Thread ID:" << rtc::Thread::Current()->GetId() << std::endl;  
  6.     mainThread = rtc::Thread::Current();  
  7. }  
  8.   
  9. void MyClass::OneTask(rtc::Thread *threadint x)  
  10. {  
  11.     invoker->AsyncInvoke<void>(RTC_FROM_HERE, thread, rtc::Bind(&MyClass::AnotherAsyncTask, (MyClass*)this, x));  
  12. }  
  13. void MyClass::AnotherAsyncTask(int x)  
  14. {  
  15.     std::cout << "Worker Thread ID:" << rtc::Thread::Current()->GetId() << std::endl;  
  16.     std::cout << "Input Value Is:" << x << std::endl;  
  17.   
  18.     invoker->AsyncInvoke<void>(RTC_FROM_HERE, mainThread, rtc::Bind(&MyClass::myFunction, (MyClass*)this, x));  
  19. }  
  20.   
  21. void MyClass::myFunction(int x)  
  22. {  
  23.     std::cout << "myFunction Thread ID:" << rtc::Thread::Current()->GetId() << std::endl;  
  24.     std::cout << "myFunction Value Is:" << x << std::endl;  
  25. }  
main.cpp

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "myclass.h"  
  2. int main()  
  3. {  
  4.     std::unique_ptr<rtc::Thread> myThread = rtc::Thread::Create();  
  5.     myThread->Start();  
  6.     MyClass *myClass = new MyClass;  
  7.     myClass->OneTask(myThread.get(), 10);  
  8.       
  9.     Sleep(10000);  
  10.   
  11.     return 0;  
  12. }  
函数AnotherAsyncTask中的invoker->AsyncInvoke<void>(RTC_FROM_HERE, mainThread, rtc::Bind(&MyClass::myFunction, (MyClass*)this, x));是想在mainThread中执行myFunction,但是myFunction并未执行,打印结果依然和上图相同。

这里需要将main.cpp稍作修改,添加代码实现线程的消息循环,新的main.cpp如下所示。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "myclass.h"  
  2. int main()  
  3. {  
  4.     std::unique_ptr<rtc::Thread> myThread = rtc::Thread::Create();  
  5.     myThread->Start();  
  6.     MyClass *myClass = new MyClass;  
  7.     myClass->OneTask(myThread.get(), 10);  
  8.       
  9.     while (true)  
  10.     {  
  11.         rtc::Thread::Current()->ProcessMessages(0);  
  12.         rtc::Thread::Current()->SleepMs(1);  
  13.     }  
  14.   
  15.     return 0;  
  16. }  
打印结果如下图所示。

从上图输出的线程ID可以看出,函数myFunction是在主线程中执行的。

0 0