Using thread with PJSUA initialization and shutdown

To use PJSIP, it is recommended to call pj_init() and pj_shutdown() from the main thread. After pj_init() is completed, application can continue with the initialization or create a secondary/worker thread and register the thread by calling pj_thread_register()

Creating a secondary thread is especially recommended, sometimes necessary, for apps that require main thread to be responsive, such as GUI apps, or apps that use video, or real-time apps.

As described in Basic API documentation, app needs to call pjsua_create(), pjsua_pool_create(), pjsua_init() to perform the initialization. Then app must call pjsua_start() to start PJSUA and finally after everything is done, call pjsua_destroy() to shut it down. Sample code:

int main()
    // Continue with PJSUA initialization here or create a secondary thread
    // After pjsua_destroy() is called

int worker_thread()
    // Register the thread, after pj_init() is called

    // Create pjsua and pool

    // Init pjsua

    // Start pjsua


    // Destroy pjsua

When restarting the library, after pjsua_destroy() is completed, application needs to call pj_shutdown() and pj_init() in the main thread.

Application also needs to make sure that the number of calls to pj_shutdown() matches with the calls to pj_init().