Using libmysqlclient in multi-threaded application

2019-02-20 03:09发布

I am building a C application on Linux platform. I need to use libmysqlclient for interfacing to database.

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:

libmysqlclient.a     libmysqlclient.so  libmysql.so.16
libmysqlclient_r.so  libmysql.so        libmysql.so.16.0.0

If my application is multi-threaded, can I link my application with libmysqlclient.a? As per mysql documentation (http://forge.mysql.com/wiki/Autotools_to_CMake_Transition_Guide), with cmake tool, clients are always thread safe.

After linking my application with libmysqlclient.a, I get a crash in my application with below call stack:

#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 ()

In my application, I have below code in the thread function:

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);

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?

Update:

Without doing anything else, I just changed mysql client build type to debug. Now I get the crash in mysql_init() function.

On the application console, I get below print:

safe_mutex: Trying to lock unitialized mutex at /install/mysqlconnc/mysql-connector-c-6.0.2/mysys/safemalloc.c, line 520

The call stack of the crash is as below:

#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 <Address 0x6d617266 out of bounds>, file = 0x65685f65 <Address 0x65685f65 out of bounds>,
          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

Solution:

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.

1条回答
何必那么认真
2楼-- · 2019-02-20 03:34

Update:

It seems that you need to call mysql_library_init() before mysql_init():

You must either call mysql_library_init() prior to spawning any threads, or else use a mutex to protect the call, whether you invoke mysql_library_init() or indirectly through mysql_init(). Do this prior to any other client library call.

Regarding your original question, libmysqlclient_r.so is actually a symbolic link to libmysql.so. You can change libmysql/CMakeLists.txt to produce a static library (libmysql.a) instead by removing the SHARED keyword from the following line:

ADD_LIBRARY(libmysql          SHARED ${CLIENT_SOURCES} libmysql.def)

However, I would recommend (1) trying to run the same code without using threads and see if the problem persists, (2) building and using the debug version of the libraries:

cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug
make

This way you could investigate the problem in more details.

查看更多
登录 后发表回答