title: Python multi-thread multi-interpreter C API
body: <p>I'm playing around with the C API for Python, but it is quite difficult to understand some corner cases. I could test it, but it seems a bug-prone and time consuming. So I come here to see if somebody has already done this.</p>
  
  <p>The question is, which is the correct way to manage a multi-thread with sub-interpreters, with no direct relation between threads and sub-interpreters?</p>
  
  <pre><code>Py_Initialize();
  PyEval_InitThreads(); /* &lt;-- needed? */
  _main = PyEval_SaveThread(); /* &lt;-- acquire lock? does it matter? */
  /* maybe do I not need it? */
  i1 = Py_NewInterpreter();
  i2 = Py_NewInterpreter();
  </code></pre>
  
  <p>Do I use a mutex? Is required to use locks? The threaded function should be something like the following: (The threads are non-python, probably POSIX threads)</p>
  
  <p><strong>Thread1</strong></p>
  
  <pre><code>_save = PyThreadState_Swap(i1);
    // python work 
  PyThreadState_Restore(_save);
  </code></pre>
  
  <p><strong>Thread2</strong> (almost identical)</p>
  
  <pre><code>_save = PyThreadState_Swap(i1);
    // python work 
  PyThreadState_Restore(_save);
  </code></pre>
  
  <p><strong>Thread3</strong> (almost identical, but with the sub-interpreter <code>i2</code>)</p>
  
  <pre><code>_save = PyThreadState_Swap(i2);
    // python work 
  PyThreadState_Restore(_save);
  </code></pre>
  
  <p>Is this correct? Is this the general case for which I want to achieve? Are there race conditions?</p>
  
  <p>Thanks!</p>