WebRTC在asyncinvoker.h和asyncivoker.cpp中实现了函数的异步履行。asyncinvoker.h的注释中给出了1个小例子,本文的学习就是从这个例子开始的。但是这个例子和单元测试代码都只演示了如何让1个函数在子线程中履行,以下所示。
myclass.h
#include "webrtc/base/asyncinvoker.h" #include <iostream> #include <memory> class MyClass { public: MyClass(); void OneTask(rtc::Thread *thread, int x); void AnotherAsyncTask(int x); private: std::unique_ptr<rtc::AsyncInvoker> invoker; };myclass.cpp
#include "myclass.h" MyClass::MyClass() { invoker.reset(new rtc::AsyncInvoker()); std::cout << "Main Thread ID:" << rtc::Thread::Current()->GetId() << std::endl; } void MyClass::OneTask(rtc::Thread *thread, int x) { invoker->AsyncInvoke<void>(RTC_FROM_HERE, thread, rtc::Bind(&MyClass::AnotherAsyncTask, (MyClass*)this, x)); } void MyClass::AnotherAsyncTask(int x) { std::cout << "Worker Thread ID:" << rtc::Thread::Current()->GetId() << std::endl; std::cout << "Input Value Is:" << x << std::endl; }main.cpp
#include "myclass.h" int main() { std::unique_ptr<rtc::Thread> myThread = rtc::Thread::Create(); myThread->Start(); MyClass *myClass = new MyClass; myClass->OneTask(myThread.get(), 10); Sleep(10000); return 0; }
打印结果
从上图输出的线程ID可以看出,函数AnotherAsyncTask是在子线程中履行的。
但是如何在子线程中调用1个函数,并让该函数中主线程中履行呢,可以将上述代码稍作更改,以下所示。
myclass.h
#include "webrtc/base/asyncinvoker.h" #include <iostream> #include <memory> class MyClass { public: MyClass(); void OneTask(rtc::Thread *thread, int x); void AnotherAsyncTask(int x); void myFunction(int x); private: std::unique_ptr<rtc::AsyncInvoker> invoker; rtc::Thread *mainThread; };myclass.cpp
#include "myclass.h" MyClass::MyClass() { invoker.reset(new rtc::AsyncInvoker()); std::cout << "Main Thread ID:" << rtc::Thread::Current()->GetId() << std::endl; mainThread = rtc::Thread::Current(); } void MyClass::OneTask(rtc::Thread *thread, int x) { invoker->AsyncInvoke<void>(RTC_FROM_HERE, thread, rtc::Bind(&MyClass::AnotherAsyncTask, (MyClass*)this, x)); } void MyClass::AnotherAsyncTask(int x) { std::cout << "Worker Thread ID:" << rtc::Thread::Current()->GetId() << std::endl; std::cout << "Input Value Is:" << x << std::endl; invoker->AsyncInvoke<void>(RTC_FROM_HERE, mainThread, rtc::Bind(&MyClass::myFunction, (MyClass*)this, x)); } void MyClass::myFunction(int x) { std::cout << "myFunction Thread ID:" << rtc::Thread::Current()->GetId() << std::endl; std::cout << "myFunction Value Is:" << x << std::endl; }main.cpp
#include "myclass.h" int main() { std::unique_ptr<rtc::Thread> myThread = rtc::Thread::Create(); myThread->Start(); MyClass *myClass = new MyClass; myClass->OneTask(myThread.get(), 10); Sleep(10000); return 0; }函数AnotherAsyncTask中的invoker->AsyncInvoke<void>(RTC_FROM_HERE, mainThread, rtc::Bind(&MyClass::myFunction, (MyClass*)this, x));是想在mainThread中履行myFunction,但是myFunction并未履行,打印结果仍然和上图相同。
这里需要将main.cpp稍作修改,添加代码实现线程的消息循环,新的main.cpp以下所示。
#include "myclass.h" int main() { std::unique_ptr<rtc::Thread> myThread = rtc::Thread::Create(); myThread->Start(); MyClass *myClass = new MyClass; myClass->OneTask(myThread.get(), 10); while (true) { rtc::Thread::Current()->ProcessMessages(0); rtc::Thread::Current()->SleepMs(1); } return 0; }打印结果以下图所示。
从上图输出的线程ID可以看出,函数myFunction是在主线程中履行的。