希望有人可以帮助我,因为我遇到了一些“奇怪”的分段错误。我目前正在开发一个 C++ 守护程序,它定期处理来自输入源的传入数据,然后将其发送回不同的系统。
守护进程当前运行两个线程——一个线程(主线程)从消息队列读取传入数据并将处理后的数据插入 MySQL 数据库,第二个线程从特定表读取并将处理后的数据推送到不同的系统。
我假设错误在某种程度上与 MySQL 和线程有关,因为我在运行一个线程时没有任何问题(只是数据处理,或者只是推回处理过的数据)。
SIGSEGV 错误都与 mysql++ 相关(连接到数据库后调用的第一个 mysql++ 方法),例如:
(gdb) backtrace
#0 clear (this=0xabd4e8) at /usr/include/c++/4.7/bits/basic_string.h:801
#1 ping (this=0xabcf40) at ./lib/dbdriver.h:465
#2 mysqlpp::Connection::ping (this=<optimized out>) at ./lib/connection.cpp:243
当两个线程都连接到 MySQL 数据库时,崩溃也总是从辅助线程触发(当主线程运行人工 while(true) 循环时运行良好)
连接代码:
mysqlConnection = new mysqlpp::Connection(false);
mysqlConnection->set_option(new mysqlpp::ReconnectOption(true)); // Reconnect if session times out
if(!mysqlConnection->connect("fakeDbName", "fakeHostName",
"fakeUserName", "fakePassword))
{
printf("Failed to connect..\n");
}
if(!mysqlConnection->thread_aware())
{
printf("Mysql++ not compiled with threading support\n");
}
else
{
printf("Mysql++ compiled with threading support\n");
}
我遵循了 Mysql++ 的指导方针,使用“--enable-thread-check”配置/编译并使用“libmysqlclient_r”链接。连接后,我将得到一个“使用线程支持编译的 Mysql++”printf。我还为两个线程创建了一个单独的连接对象。
有什么想法,有人吗?