Note that there are some explanatory texts on larger screens.

plurals
  1. POlibmysqlclient race condition
    primarykey
    data
    text
    <p>I have multiple threads that need to connect to a single instance of MySQL server. If every thread creates and handles its own connection everything works out as expected. I wanted to implement a simple connection pool as this :</p> <pre><code>void query_user(MYSQL* conn) { mysql_thread_init(); std::ostringstream query; query &lt;&lt; "select * from user u where u.username = '" &lt;&lt; "stack" &lt;&lt; "'"; if (mysql_query(conn, query.str().c_str())) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(-1); } MYSQL_RES* res = mysql_use_result(conn); MYSQL_ROW row; while ((row = mysql_fetch_row(res)) != NULL) { printf("%s %s %s %s %s %s %s %s %s\n", row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]); } mysql_free_result(res); mysql_thread_end(); } #define POOL_SIZE 2 int main(int argc, char** argv) { mysql_library_init(argc, argv, NULL); // Create Connection pool MYSQL* connection_pool[POOL_SIZE]; for (size_t i = 0; i &lt; sizeof(connection_pool)/sizeof(MYSQL*); ++i) { connection_pool[i] = mysql_init(NULL); if (!mysql_real_connect(connection_pool[i], server, user, password, database, 0, NULL, 0)) { fprintf(stderr, "%s\n", mysql_error(connection_pool[i])); exit(-1); } } // Thread Pool pthread_t thread_pool[POOL_SIZE]; for (size_t i = 0; i &lt; sizeof(thread_pool)/sizeof(pthread_t); ++i) { pthread_create(&amp;thread_pool[i], NULL, reinterpret_cast&lt;void* (*)(void*)&gt;(query_user), (void*)&amp;connection_pool[i]); } // join threads for (size_t i = 0; i &lt; sizeof(thread_pool)/sizeof(pthread_t); ++i) { pthread_join(thread_pool[i], NULL); mysql_close(connection_pool[i]); } mysql_library_end(); return 0; } </code></pre> <p>I have a bus error in mysql_real_query</p> <p>Could you please point out what's wrong with it ? Are there still some benefits from connection pooling ?</p> <p>I'm linking against libmysqlclient_r which is supposed to be thread safe</p> <p>Thanks,</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload