Search code examples
clinuxmultithreadingpthreadslibmysql

Multithreaded programming with libmysql


I'm using libmysql in a simple multithreaded app which will run on a uni-core embedded system processor. I read here that the client library is almost thread-safe.

Do I need to serialise my app (say, with a mutex)?


Solution

  • Depends on what you're doing. In a simple scenario you'd just link against libmysqlclient_r and make sure you don't share a connection with multiple threads nor execute multiple queries "simultaneously" on a single connection.

    Other requirements:

    1. Before creating any threads, call mysql_library_init() to initialise the MySQL library;
    2. On each thread call mysql_thread_init() to initialise thread-specific variables before using any MySQL related functions;
    3. Before destroying a thread, call mysql_thread_end().

    If your program is respecting these limitations, MySQL is your friend.