title: Using libmysqlclient in multi-threaded application
body: <p>I am building a C application on Linux platform. I need to use libmysqlclient for interfacing to database.</p>
  
  <p>I downloaded Linux source code package mysql-connector-c-6.0.2.tar.gz. I compiled it as per the instructions. I get the below libraries:</p>
  
  <pre><code>libmysqlclient.a     libmysqlclient.so  libmysql.so.16
  libmysqlclient_r.so  libmysql.so        libmysql.so.16.0.0
  </code></pre>
  
  <p>If my application is multi-threaded, can I link my application with libmysqlclient.a? As per mysql documentation (<a href="http://forge.mysql.com/wiki/Autotools_to_CMake_Transition_Guide" rel="nofollow">http://forge.mysql.com/wiki/Autotools_to_CMake_Transition_Guide</a>), with cmake tool, clients are always thread safe.</p>
  
  <p>After linking my application with libmysqlclient.a, I get a crash in my application with below call stack:</p>
  
  <pre><code>#0  0x0867878a in my_stat ()
  No symbol table info available.
  #1  0x08671611 in init_available_charsets.clone.0 ()
  No symbol table info available.
  #2  0x086720d5 in get_charset_by_csname ()
  No symbol table info available.
  #3  0x086522af in mysql_init_character_set ()
  No symbol table info available.
  #4  0x0865266d in mysql_real_connect ()
  </code></pre>
  
  <p>In my application, I have below code in the thread function:</p>
  
  <pre><code>if (NULL == (pMySQL = mysql_init(NULL)))
  {
      return -1;
  }
  
  if (NULL == mysql_real_connect(pMySQL, ServerName, UserName, Password, Name, Port, NULL, 0))
  {
      mysql_close(pMySQL);
      return -1;  
  }
  
  if (0 != mysql_query(pMySQL, pQuery))
  {
      mysql_close(pMySQL);
      return -1;
  }
  
  mysql_close(pMySQL);
  </code></pre>
  
  <p>I am not using libmysqlclient_r.so as I want to link to mysql client library statically. Is there any way to generate libmysqlclient_r.a with cmake?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Without doing anything else, I just changed mysql client build type to debug. Now I get the crash in mysql_init() function.</p>
  
  <p>On the application console, I get below print:</p>
  
  <pre><code>safe_mutex: Trying to lock unitialized mutex at /install/mysqlconnc/mysql-connector-c-6.0.2/mysys/safemalloc.c, line 520
  </code></pre>
  
  <p>The call stack of the crash is as below:</p>
  
  <pre><code>#0  0x00556430 in __kernel_vsyscall ()
  No symbol table info available.
  #1  0x45fdf2f1 in raise () from /lib/libc.so.6
  No symbol table info available.
  #2  0x45fe0d5e in abort () from /lib/libc.so.6
  No symbol table info available.
  #3  0x086833e5 in safe_mutex_lock (mp=0x915e8e0, my_flags=0, 
      file=0x895b9d8 "/install/mysqlconnc/mysql-connector-c-6.0.2/mysys/safemalloc.c", line=520)
      at /install/mysqlconnc/mysql-connector-c-6.0.2/mysys/thr_mutex.c:178
          error = 140915306
          __PRETTY_FUNCTION__ = "safe_mutex_lock"
  #4  0x08682715 in _sanity ( 
      filename=0x895a87c "/install/mysqlconnc/mysql-connector-c-6.0.2/mysys/my_error.c", lineno=195)
      at /install/mysqlconnc/mysql-connector-c-6.0.2/mysys/safemalloc.c:520
          irem = 0xf2300468
          flag = 0
          count = 0
  #5  0x0868186b in _mymalloc (size=16,
      filename=0x895a87c "/install/mysqlconnc/mysql-connector-c-6.0.2/mysys/my_error.c", lineno=195, MyFlags=16)
      at /install/mysqlconnc/mysql-connector-c-6.0.2/mysys/safemalloc.c:130
          irem = 0x0
          data = 0x0
          _db_stack_frame_ = {func = 0x6d617266 &lt;Address 0x6d617266 out of bounds&gt;, file = 0x65685f65 &lt;Address 0x65685f65 out of bounds&gt;,
            level = 0, prev = 0x0}
  #6  0x0867e0e1 in my_error_register (errmsgs=0x89a7760, first=2000, last=2058)
      at /install/mysqlconnc/mysql-connector-c-6.0.2/mysys/my_error.c:194
          meh_p = 0x46087568
          search_meh_pp = 0x1000
  #7  0x08655f7e in init_client_errs ()
      at /install/mysqlconnc/mysql-connector-c-6.0.2/libmysql/errmsg.c:238
  No locals.
  #8  0x08655fe3 in mysql_server_init (argc=0, argv=0x0, groups=0x0)
      at /install/mysqlconnc/mysql-connector-c-6.0.2/libmysql/libmysql.c:128
          result = 0
  #9  0x08651fc0 in mysql_init (mysql=0x0)
      at /install/mysqlconnc/mysql-connector-c-6.0.2/libmysql/client.c:1606
  </code></pre>
  
  <p><strong>Solution:</strong></p>
  
  <p>I put a call to mysql_library_init() before creation of threads and put a call to mysql_library_end() after termination of threads. In each thread, I put a call to mysql_thread_init() at the start of thread function and put a call to mysql_thread_end() at the end of thread function. This solved the problem of crashing.</p>