3
gcc 4.7.2
c89
apr utility 1.4

你好,

我正在使用线程池来启动线程。但是,我看不到任何允许我等待线程加入的 apr 函数。

代码 sippet,删除了所有错误检查和非必要部分:

int main(void)
{
    /* Initialize apr internal structures */
    apr_initialize();

    /* Create memory pool */
    rv = apr_pool_create(&mem_pool, NULL);

    /* Create thread pool */
    memset(&buf, 0, sizeof buf);
    rv = apr_thread_pool_create(&thd_pool,
                                init_threads,
                                max_threads,
                                mem_pool);

    /* Process the number of jobs */
#define NUMBER_JOBS 1
    for(i = 0; i < NUMBER_JOBS; i++) {
        rv = apr_thread_pool_schedule(thd_pool,
                                      timeout_duration,
                                      (void*)channel,
                                      (apr_interval_time_t)flash_timeout,
                                      NULL);

    }

    /* 
     * Join all threads here 
     */

    /* Destroy resources */
    apr_thread_pool_destroy(thd_pool);
    apr_pool_destroy(mem_pool);
    apr_terminate();

    return 0;
error:
    apr_thread_pool_destroy(thd_pool);
    apr_pool_destroy(mem_pool);
    apr_terminate();

    return 1;
}

void* timeout_duration(apr_thread_t *thd, void *data)
{
    channel_t *channel = (channel_t*)data;

    LOG_DEBUG("Channel timeout notification [ %zu ]", channel->id);
}

我看不到任何加入线程的 apr utity 函数。

但是,我确实找到了这个函数apr_thread_join(apr_status_t *retval, apr_thread_t *thd)但是,它需要 aapr_thread_t作为参数。

函数 timeout_duration 需要 aapr_thread_t但是如果我需要使用它来加入,我该如何设法将其传回?

只是一个旁注问题。是否有任何使用 apr 的示例项目,我可以参考。文档非常有限。

非常感谢您的任何建议,

4

1 回答 1

4

短句

You don't need to join the threads in the thread-pool. When you call apr_thread_pool_destroy the function will block, until all threads have finished their current task.

To answer your last question first: I didn't find an example but the libapr and libapr-util are open-source, you can read the source and this is what I did: (I checked the SVN-trunk here rev 1441871)

Long answer

The interesting files:

First check in apr_thread_pool.c:394. Here we find the implementation of apr_thread_pool_destroy. We can see the it calls a function called apr_pool_cleanup_run with three arguments, one is the pool-storage, one is the thread-pool-context and the last one is a function-pointer to the function thread_pool_cleanup.

If we follow apr_pool_cleanup_run we will get to apr_pools.c:2453 and see that apr_pool_cleanup_kill is called. Reading this last function shows us, that here in several loops over the elements (the threads) are cleaned by (what we will see later) calling the cleanup_fn-function-argument.

Now back in function apr_pool_cleanup_run there is a final call to cleanup_fn.

The real action is going on the function-pointer passed to apr_pool_cleanup_run. So, if we go back to apr_thread_pool.c:329 we find the function thread_pool_cleanup.

In it the context-variable terminated is set to 1 and then the function is "sleeping" until _myself->thd_cnt has become 0.

Searching for the usage of terminated we find that thread_pool_func is exiting its loop when terminated is not 0. It turns out that thread_pool_func is the function each thread in the threadpool is using. In the loop a task is fetched and executed. When the loop has terminated (because terminated has become 1) the following code is executed:

 /* idle thread been asked to stop, will be joined */
 --me->thd_cnt;

This will ultimately lead to the thd_cnt==0 which is the terminate-condition for the loop in thread_pool_cleanup.

When you call apr_thread_pool_destroy all threads are stopped cleanly before the function returns.

于 2013-02-03T08:12:46.927 回答