Skip to content

Commit 3274da7

Browse files
committed
Improve the abstract.
1 parent 32c2c79 commit 3274da7

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

peps/pep-0788.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,40 @@ correct and safer replacement for :c:func:`PyGILState_Ensure` and
4949
5050
PyThreadState_Release();
5151
52+
This is achieved by introducing two concepts into the C API:
53+
54+
- "Daemon" and "non-daemon" threads, similar to how it works in the
55+
:mod:`threading` module.
56+
- Interpreter reference counts which prevent the interpreter from finalizing.
57+
58+
In :c:func:`PyThreadState_Ensure`, both of these ideas are applied. The
59+
calling thread is to store a reference to the interpreter via
60+
:c:func:`PyInterpreterState_Hold`. :c:func:`PyInterpreterState_Hold`
61+
increases the reference count of the interpreter, requiring the thread
62+
to finish (by eventually calling :c:func:`PyThreadState_Release`) before
63+
beginning finalization.
64+
65+
As such, creating a native thread with this API would look something
66+
like this:
67+
68+
.. code-block:: c
69+
70+
static PyObject *
71+
my_method(PyObject *self, PyObject *unused)
72+
{
73+
PyThread_handle_t handle;
74+
PyThead_indent_t indent;
75+
76+
PyInterpreterState *interp = PyInterpreterState_Hold();
77+
if (PyThread_start_joinable_thread(thread_func, interp, &ident, &handle) < 0) {
78+
PyInterpreterState_Release(interp);
79+
return NULL;
80+
}
81+
/* The thread will always attach and finish, because we increased
82+
the reference count of the interpreter. */
83+
Py_RETURN_NONE;
84+
}
85+
5286
Motivation
5387
==========
5488

0 commit comments

Comments
 (0)