Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As maintainer of a fairly large C application that makes MySQL calls from multiple threads, I can say I've had no problems with simply making a new connection in each thread. Some caveats that I've come across:</p> <ul> <li><strong>Edit: it seems this bullet only applies to versions &lt; 5.5; see <a href="http://dev.mysql.com/doc/refman/5.7/en/c-api-threaded-clients.html" rel="noreferrer">this page for your appropriate version</a>:</strong> Like you say you're already doing, link against <code>libmysqlclient_r</code>.</li> <li>Call <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-library-init.html" rel="noreferrer"><code>mysql_library_init()</code></a> (once, from <code>main()</code>). Read the docs about use in multithreaded environments to see why it's necessary.</li> <li>Make a new <code>MYSQL</code> structure using <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-init.html" rel="noreferrer"><code>mysql_init()</code></a> in each thread. This has the side effect of calling <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-thread-init.html" rel="noreferrer"><code>mysql_thread_init()</code></a> for you. <code>mysql_real_connect()</code> as usual inside each thread, with its thread-specific MYSQL struct.</li> <li>If you're creating/destroying lots of threads, you'll want to use <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-thread-end.html" rel="noreferrer"><code>mysql_thread_end()</code></a> at the end of each thread (and <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-library-end.html" rel="noreferrer"><code>mysql_library_end()</code></a> at the end of <code>main()</code>). It's good practice anyway.</li> </ul> <p>Basically, don't share <code>MYSQL</code> structs or anything created specific to that struct (i.e. <code>MYSQL_STMT</code>s) and it'll work as you expect.</p> <p>This seems like less work than making a connection pool to me.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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