예제 #1
0
static void
ngx_thread_pool_destroy(ngx_thread_pool_t *tp)
{
    ngx_uint_t           n;
    ngx_thread_task_t    task;
    volatile ngx_uint_t  lock;
    ngx_memzero(&task, sizeof(ngx_thread_task_t));
    task.handler = ngx_thread_pool_exit_handler;
    task.ctx = (void *) &lock;
    for (n = 0; n < tp->threads; n++)
    {
        lock = 1;
        if (ngx_thread_task_post(tp, &task) != NGX_OK)
        {
            return;
        }
        while (lock)
        {
            ngx_sched_yield();
        }
        task.event.active = 0;
    }
    (void) ngx_thread_cond_destroy(&tp->cond, tp->log);
    (void) ngx_thread_mutex_destroy(&tp->mtx, tp->log);
}
static void
ngx_thread_pool_destroy(ngx_thread_pool_t *tp)
{
    ngx_uint_t           n;
    ngx_thread_task_t    task;
    volatile ngx_uint_t  lock;

    ngx_memzero(&task, sizeof(ngx_thread_task_t));

    task.handler = ngx_thread_pool_exit_handler;//任务退出执行函数
    task.ctx = (void *) &lock;//指向传入的参数  

    // 没有赋值task->event.handler  task->event.data   (void) ngx_notify(ngx_thread_pool_handler); 中会不会段错误??? /
    for (n = 0; n < tp->threads; n++) {  //tp中所有的线程池添加该任务  
        lock = 1;

        if (ngx_thread_task_post(tp, &task) != NGX_OK) {
            return;
        }

        while (lock) { //主进程判断如果lock没有改变,就让CPU给其他线程执行,以此等待,相当于pthread_join  
            ngx_sched_yield();
        }

        //只有线程池中的一个线程执行了exit_handler后才能会继续for循环

        task.event.active = 0;
    }

    //此时到这边,所有的线程都已经退出   //条件变量销毁   互斥锁

    (void) ngx_thread_cond_destroy(&tp->cond, tp->log);

    (void) ngx_thread_mutex_destroy(&tp->mtx, tp->log);
}