C++ SDK Thread Safe Connection
Posted in 2024
Hi, I'm developing an application multi-thread that needs to connect to an Informix DB. I'm working with the CSDK 4.50 and c++ libraries. I'm encountering issues with the connection. I didn't find the way in which initialize a different connection for every thread. here a simple snippet of code for replicate my issues int main(int argc, char *argv[]) { const constexpr int nthreads = 10; std::thread t[nthreads]; for (int i = 0; i < nthreads; ++i) { t[i] = std::thread(printArt, conn, dbinfo, i); } for (int i = 0; i < nthreads; ++i) { t[i].join(); } return 0; } void printArt(int i) { std::random_device rd; // obtain a random number from hardware std::mt19937 gen(rd()); // seed the generator std::uniform_int_distribution<> distr(2000, 5000); // define the range vector<string> result; ITDBInfo dbinfo; dbinfo.SetDatabase("eda"); ITConnection conn; conn.Open(dbinfo) if (conn.Error()) { return; } conn.SetTransaction(ITConnection::Begin) ITCursor cursor(conn); if (!cursor.Prepare("select first 1 * from a_table;")) { conn.SetTransaction(ITConnection::ABORT); return; } if (!cursor.Open()) { conn.SetTransaction(ITConnection::ABORT); return; } std::this_thread::sleep_for(std::chrono::milliseconds(distr(gen))); ITRow* row = nullptr; while ((row = cursor.NextRow()) != nullptr) { string col0 = row->Column(0)->Printable().Data(); string col1 = row->Column(1)->Printable().Data(); cout << "[" << i << "] " << col0 << " | " << col1 << endl; row->Release(); } conn.SetTransaction(ITConnection::Commit) conn.Close(); return; } The result of this execution is undefined, sometimes it goes ahead until the end, sometimes it crash with a core dumped. When the program doesn't crash it seems that only one query has result and print it. The expected result is to see 1 line printed from every query (thread). Could anyone say me where is the mistake? Thanks, Luigi ------------------------------ Luigi Roggio ------------------------------