[C++-sig] Boost.Python and thread safety

Davidson, Josh josh.davidson at lmco.com
Wed Oct 29 16:34:28 CET 2014


Hi all, I'm trying to determine if the following constitutes a thread safety violation with Python.

To simplify the discussion, let's say have the following fictional classes:
* Teacher - Fully implemented in Python
* Student - Implemented in C++ with Boost.Python bindings
* Quiz - Implemented in C++ with Boost.Python bindings


In a Python interpreter, a single instance of a teacher is instantiated and multiple instances of students are instantiated (via the bindings).  The teacher then instantiates a number of quiz objects (via the bindings) and invokes a method on the student class that takes a reference to a quiz object.  The teacher objects kicks off a Python thread that blocks until all students are completed with the quiz.  The student objects set attributes on their assigned quiz objects until they are done.  

However, if during the course of taking the test, a student kicks of a C++ (Boost) thread, say to scan his classmates' papers, and in that non-Python thread without the GIL, invokes getters/setters on the quiz object, is that a violation of thread safety?   The quiz object is protected from concurrent access.  If the quiz object was instantiated from a C++ application and passed in there would definitely be no problem.  If the quiz object is instantiated via the bindings and passed into the C++ student, I'm not so sure since there would be a thread accessing a (wrapped) Python object from threads that don't hold the GIL.

Thanks,
Josh




More information about the Cplusplus-sig mailing list