Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add documentation note.
  • Loading branch information
ZeroIntensity committed Jan 4, 2025
commit d60f2e237d4f8ee4265c873186e3dbfb6c93a29b
29 changes: 29 additions & 0 deletions Doc/library/ctypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,35 @@ invalid non-\ ``NULL`` pointers would crash Python)::
ValueError: NULL pointer access
>>>

.. _ctypes-thread-safety:

Thread Safety Without The GIL
Comment thread
ZeroIntensity marked this conversation as resolved.
Outdated
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In Python 3.13, the :term:`GIL` may be disabled on :term:`experimental free threaded <free threading>` builds.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is orthogonal but whenever I review FT PRs, we always use :term:`free threaded `. I believe we can add a new term for free threaded which points to free threading so that we can reduce the docs burden.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A free threaded term sounds like a good idea, but that should probably be in a larger follow-up PR. I'll play around with the wording here though.

In ctypes, reads and writes to a single object concurrently is safe, but not across multiple objects.::
Comment thread
ZeroIntensity marked this conversation as resolved.
Outdated

>>> number = c_int(42)
>>> pointer_a = pointer(number)
>>> pointer_b = pointer(number)
Comment thread
ZeroIntensity marked this conversation as resolved.
Outdated

In the above, it's only safe for one object to read and write to the address at once if the :term:`GIL` is disabled.
Comment thread
ZeroIntensity marked this conversation as resolved.
Outdated
So, ``pointer_a`` can be shared and written to across multiple threads, but only if ``pointer_b``
is not also attempting to do the same. If this is an issue, consider using a :class:`threading.Lock`
to synchronize access to memory.::
Comment thread
ZeroIntensity marked this conversation as resolved.
Outdated

>>> import threading
Comment thread
ZeroIntensity marked this conversation as resolved.
Outdated
>>> lock = threading.Lock()
>>> # Thread 1
>>> with lock:
... pointer_a.contents = 24
>>> # Thread 2
>>> with lock:
... pointer_b.contents = 42

.. seealso::

:func:`sys._is_gil_enabled` if you would like to dynamically synchronize your application.
Comment thread
ZeroIntensity marked this conversation as resolved.
Outdated

.. _ctypes-type-conversions:

Expand Down