linux shared library to support multi threaded application callbacks

I need to create a shared library which exposes a set of APIs which will be used by multiple processes which might have more than one thread from where the APIs get called.

This shared library in turn uses another 3rd party shared library to which I need to register a callback. The 3rd party library calls the registered callback from a different thread.

I want to know how to block a thread on a call of API defined in my library and release it when I get the callback from the 3rd party library. This locking should not block other thread from calling the same API…!

I’m using pthread library to create my library.

Psudo Code:

My Library:

int lib_init()
{
    register_callback(callback);
}

int lib_deinit()
{
    unregister_callback();
}

int callback(void *)
{
    <unblock the functions>
}

int function1(int, int)
{
    perform_action();
    <should block till I get a callback from 3rd party library>
}

int function2(char, char)
{
    perform_action();
    <should block till I get a callback from 3rd party library>
}

3rd Party Library:

int register_callback(callback)
{
    ....
    launch_thread();
    ....
}

int unregister_callback()
{
    ....
    terminate_thread();
    ....
}

int perform_action()
{
    /* queue action */
}

void* thread(void*arg)
{
    /* perform the action & invoke callback */
    invoke_callback();
}

Application:

main()
{
    init_lib();
    ....
    create_new_thread();
    ....
    function1(10, 20);
    ....
    function2('c', 'd');
}

another_thread()
{
    function2('a', 'b');
    ....
}

The exact problem I’m not able to solve is what(how) locking mechanism I need to put in place to block the calls to functions defined in my library & wait for callback from 3rd party library provided my library shall be used in a multi-process & multi-threaded environment.


Source: c++

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.