tls pjsua app 分析 pjsua(9)

电脑杂谈  发布时间:2018-02-09 00:05:23  来源:网络整理
static long thread_tls_id = -1;
pj_status_t pj_thread_init(void){
    rc = pj_thread_local_alloc(&thread_tls_id);
    return pj_thread_register("thr%p", main_thread, &thread);
}

PJ_DEF(pj_status_t) pj_thread_local_alloc(long *index){
    *index = TlsAlloc();//关联TLS 数组的slot
   //1.一个线程使用TLS时,第一步程内调用TlsAlloc()函数,TLS数组index与当前线程的TLS数组的槽(slot)关联起来
   //2.为当前线程动态分配一块内存区域(使用LocalAlloc),然后把指向这块内存区域的指针放入TLS数组相应的槽中
   //3.在当前线程过TLS数组的index,使用TlsGetValue()函数得到内存区域的指针,然后进行内存区域的读写操作了
   //4.动态释放掉这块内存区域(使用LocalFree()函数),然后从TLS数组中放弃对应的槽(使用TlsFree()函数

}

这里有个分析线程注册的流程,首先要看参数 main_thread 和 thread

pj_thread_t *thread;
struct pj_thread_t{
    char        obj_name[PJ_MAX_OBJ_NAME];
    HANDLE      hthread;//句柄,是Windows用来表示对象的 系统占用资源的唯一索引
    DWORD       idthread; //占4个字节
    pj_thread_proc *proc;
    void       *arg;
};
static pj_thread_desc main_thread;
typedef long pj_thread_desc[PJ_THREAD_DESC_SIZE];//64

知道了上面的参数定义,就可以研究如何注册了,将线程句柄和线程id 绑定起来

PJ_DEF(pj_status_t) pj_thread_register ( const char *cstr_thread_name,pj_thread_desc desc,
                                         pj_thread_t **thread_ptr){
    pj_thread_t *thread = (pj_thread_t *)desc; //main_thread
    pj_str_t thread_name = pj_str((char*)cstr_thread_name);//"thr%p"

    /* Initialize and set the thread entry. */
    pj_bzero(desc, sizeof(struct pj_thread_t));
    thread->hthread = GetCurrentThread(); //获取当前线程的伪句柄
    thread->idthread = GetCurrentThreadId();//获取当前线程的线程id  
    rc = pj_thread_local_set(thread_tls_id, thread);
    //rc = TlsSetValue(index, value);  //绑定 线程index和线程
    //https://www.cnblogs.com/stli/archive/2010/11/03/1867852.html 博客讲的挺不错
    *thread_ptr = thread;
    return PJ_SUCCESS; //这就算线程注册完毕
}


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-67841-9.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 陈子昂
      陈子昂

      感动

    • 黄泽宇
      黄泽宇

      )超爱MV细腻动人

    • 张元鹏
      张元鹏

      经济上他拿我们没有办法只能一天天看着我们追上他超过他

    热点图片
    拼命载入中...