本文介绍了在多线程应用程序中使用libmysqlclient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux平台上构建一个C应用程序。我需要使用libmysqlclient接口到数据库。



我下载了Linux源代码包mysql-connector-c-6.0.2.tar.gz。我按照说明编译它。我得到以下库:

  libmysqlclient.a libmysqlclient.so libmysql.so.16 
libmysqlclient_r.so libmysql。所以libmysql.so.16.0.0

如果我的应用程序是多线程的,用libmysqlclient.a?根据mysql文档(),与



在使用libmysqlclient.a链接我的应用程序后,我在应用程序中遇到调用栈下面的崩溃:

 #0 0x0867878a在my_stat()
无符号表信息可用。
#1 0x08671611在init_available_charsets.clone.0()
无符号表信息可用。
#2 0x086720d5在get_charset_by_csname()
没有符号表信息可用。
#3 0x086522af在mysql_init_character_set()
没有符号表信息可用。
#4 0x0865266d在mysql_real_connect()

在我的应用程序中,线程函数:

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



我不使用libmysqlclient_r.so,因为我想链接到mysql客户端库静态。有什么方法可以用cmake生成libmysqlclient_r.a?



更新:



不做任何其他事情,我只是改变mysql客户端生成类型为调试。现在我在mysql_init()函数中崩溃。



在应用程序控制台上,我得到以下print:

  safe_mutex:尝试在/install/mysqlconnc/mysql-connector-c-6.0.2/mysys/safemalloc.c,第520行锁定单位化mutex 

崩溃的调用堆栈如下:

 #0 0x00556430在__kernel_vsyscall()
无符号表信息可用。
#1 0x45fdf2f1在raise()中从/lib/libc.so.6
无符号表信息可用。
#2 0x45fe0d5e在abort()中从/lib/libc.so.6
无符号表信息可用。
#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
错误= 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)
在/ install / mysqlconnc / mysql -connector-c-6.0.2 / mysys / safemalloc.c:520
irem = 0xf2300468
flag = 0
count = 0
#5 0x0868186b in _mymalloc ,
filename = 0x895a87c/install/mysqlconnc/mysql-connector-c-6.0.2/mysys/my_error.c,lineno = 195,MyFlags = 16)
在/ 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<地址0x65685f65超出范围>,
level = 0,prev = 0x0}
在my_error_register中为6 0x0867e0e1(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
没有本地。
#8 0x08655fe3在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在mysql_init(mysql = 0x0)
at /install/mysqlconnc/mysql-connector-c-6.0.2/libmysql/client.c:1606



解决方案:



我在创建线程之前调用mysql_library_init(),并在线程终止后调用mysql_library_end()。在每个线程中,我在线程函数的开始调用mysql_thread_init(),并在线程函数的结尾调用mysql_thread_end()。

解决方案

更新



您似乎需要致电 mysql_init()

关于您的原始问题, libmysqlclient_r.so 实际上是 libmysql.so 的符号链接。您可以更改 libmysql / CMakeLists.txt 以生成静态库( libmysql.a ), c $ c> SHARED 关键字:

  ADD_LIBRARY(libmysql SHARED $ {CLIENT_SOURCES}但是,我建议(1)尝试运行相同的代码,而不使用线程(1)尝试运行相同的代码,而不使用线程并查看问题是否仍然存在,(2)构建和使用调试版本的库:

  cmake -GUnix Makefiles -DCMAKE_BUILD_TYPE = Debug 
make

这样可以更详细地调查问题。


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.

解决方案

Update:

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

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.

这篇关于在多线程应用程序中使用libmysqlclient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:46
查看更多