ThreadManager的路径内部数据
几个重要成员
-
ThreadManager* Instance() : 单例对象
-
std::vector<Thread*> message_queues_ : 存放所有线程对象的数组
-
RecursiveCriticalSection crit_ : 临界区, 所有对 message_queues_的中的数据进行读取的时候都要先获取临界区并对其加锁, 然后才能操作 message_queues_.
-
pthread_key_t key_ : Linux/Mac下的key (WEBRTC_POSIX)
-
const DWORD key_ : windos下的key (WEBRTC_WIN)
ThreadManager管理
从上图中可以知道:
-
message_queues_中的Thread 是在Thread创建的时候加入进去的.
-
thread local storage中的thread是在当前thread执行的时候添加进去的.
-
两边的thread是一一对应的.
thread local storage (是在C++11引入的)
-
TlsAlloc(), 返回全局索引值(index), 在内存中分配一个map.
-
TlsSetValue(index,thread), 在TlsAlloc()分配的空间中插入<tid, thread>, 由于是在主线程中执行的, 所以存入的一定是当前线程的线程ID 即tid, 与要存入的线程指针形成一个key- value对 存入了分配好的内存空间中,从而实现了当前线程与thread对象的绑定.
-
TlsGetValue(), 在内存中取当前线程tid所对应的值.
Thread是如何加入到ThreadManager中的
Thread在构造时, 最终会执行到上图中的函数内, 从而this指针(Thread指针)加入到Thread manager中.