Search code examples
c++cmultithreadingaprthread-sleep

How to sleep an APR thread?


I am using APR library to create portable multi-threading program in C++. Problem is I need to sleep a thread when it is not needed but there is no function mentioned in manual to do so.

Do you now a way how to sleep an APR thread without need to use native system functions? I would like to avoid any OS specific code. Thank you.


Solution

  • If you simply want to hand over CPU to other thread, you can use:

    void apr_thread_yield(void);
    

    Otherwise, you can use:

    apr_status_t apr_thread_cond_timedwait(
            apr_thread_cond_t *     cond,
            apr_thread_mutex_t *    mutex,
            apr_interval_time_t     timeout  
        );
    

    or

    apr_status_t apr_thread_cond_wait(
            apr_thread_cond_t *     cond,
            apr_thread_mutex_t *    mutex
        );
    

    Refer to here.