我正在使用Boost线程(pthread)在C ++中编写多线程应用程序。该应用程序产生100个线程,每个线程执行以下任务(我正在编写将在每个线程中运行的代码片段):
try {
driver = get_driver_instance();
con = driver->connect(SettingsClass.HostName, \
SettingsClass.UserName,SettingsClass.Password);
// SettingsClass is a global static class whose members
// (HostName, UserName, Password, etc) are initialized once
// before *any* thread is created.
con->setSchema("MyDatabase");
driver->threadInit();
string dbQuery = "select A, B, C from XYZTable where D=?";
prepStmt = con->prepareStatement(dbQuery);
prepStmt->setInt(1, 1);
rSet = prepStmt->executeQuery();
/* Do Something With rSet, the result set */
delete rSet;
delete prepStmt;
if (con != NULL && !con->isClosed()) {
con -> close();
driver->threadEnd();
delete con;
}
catch (SQLException &e)
{
/* Log Exception */
}
在运行进程(应用程序,如前所述,即有100个这样的线程)时,我在中途附加gdb并观察到40%以上的线程在read()中挂起呼叫。所有的回溯都有mysql库函数(vio_read()等),而且我的代码都没有执行任何I / O.
有人能指出为什么会出现这个问题。我应该检查我的代码/网络或MySQL服务器配置吗?我是否正确使用了C ++连接器库?