title: MySQL C API Initialization in Multi-Threaded Program
body: <p>I'm on Linux using the MySQL 5.5 C API.</p>
  
  <p>My program starts in a single main thread and then spawns lots of worker pthreads.  The worker threads need to make database transactions.</p>
  
  <p>So there are two functions in my program:</p>
  
  <pre><code>foo_init_main_thread() // called once before any worker threads are started
  
  foo_init_worker_thread() // called from a worker thread
                           // immediately after it has started
  </code></pre>
  
  <p>It is not clear how to initialize MySQL in this enviornment.  The documentation is really unclear.</p>
  
  <p>Inside <code>foo_init_main_thread</code> and <code>foo_init_worker_thread</code> which of <code>mysql_thread_init</code>, <code>mysql_library_init</code>, <code>mysql_init</code>, <code>mysql_connect</code> do I need to call and in what order?</p>
  
  <p>Do I need to have one database handle per thread or can the worker threads all share a database handle?</p>
  
  <p>The documentation is here: <a href="http://dev.mysql.com/doc/refman/5.5/en/c.html" rel="nofollow">http://dev.mysql.com/doc/refman/5.5/en/c.html</a></p>