Zaxter
Zaxter

Reputation: 3035

Using thread-safe libraries

I can think of two ways a thread-safe library can be used:

One is having a global instance of the library protected by a mutex, which is initialised by the main thread and used by worker threads, like so:

mutex g_lib_mutex;
lib_t g_lib;

thread:
    lock(&g_lib_mutex);
    /* use lib */
    unlock(&g_lib_mutex);


main:
    lib_init(&g_lib);
    start_threads(thread);

    lock(&g_lib_mutex);
    /* use lib */
    unlock(&g_lib_mutex);

    join_threads();
    lib_close(&g_lib);

The other, is for every thread to have a local instance of the library, something like this:

thread:
    lib_t g_lib;
    lib_init(&g_lib);
    /* use lib */
    lib_close(&g_lib);


main:
    start_threads(thread);
    lib_t g_lib;
    lib_init(&g_lib);
    /* use lib */
    lib_close(&g_lib);

Which of these ways is more correct / preferable?

Do I need to protect library calls with a global mutex in both cases?

I was trying to use libmysql and POSIX message queues in a multi-threaded application when this question crossed my mind.

Upvotes: 2

Views: 1056

Answers (1)

Marcus Müller
Marcus Müller

Reputation: 36462

Generally, only initialize a library once. Remember, all threads happen in the same process' memory space, so whatever you do to any global variables in thread X is true for all threads. Library initialization should happen only once per process.

Now, whether library calls are thread safe or must be protected by mutexes is a question of your library. Modern libraries should have definite documentation on what functions you're allowed to call from multiple threads. If that info is missing you can either

  • assume the worst and encapsulate everything that changes something that the library deals with, or calls into the library, with a single global mutex, or
  • read the source code of the library to figure out what might go wrong where, introduce security measures (mutexes/conditions) accordingly, and make sure that no one uses a different version of the library (where things might be different), or
  • improve the documentation, send that patch to the upstream developers asking them to verify that what you document in thread-(un)safety is intentional and matches reality, (documentation patches are, for any project that I know of, always welcome) or
  • modify the library itself to be thread safe (making yourself a hero).

Upvotes: 2

Related Questions