[C++-sig] Thread safety

dueymk dueymk at everestkc.net
Fri Dec 16 20:36:34 CET 2005

I'm wrapping a multi-threaded C++ dll with Boost.Python.  A thread is created when a wrapped structure is subclassed and instantiated in Python. This thread spends most of it's time in C++ but calls a virtual function which may be overridden by the Python subclass.  I've done the following:

class Original
	int counter;

	virtual bool Process(int *count)
		return true;

struct PythonOriginal : Original, wrapper<Original>
	bool Process(int *count)
		if (override Process = this->get_override("Process"))
			PyGILState_STATE gstate;
			gstate = PyGILState_Ensure();
			bool answer = call<bool>(Process.ptr(), boost::ref(count));

			return answer;
			return Original::Process(count);

	bool default_Process(int *count)
		return this->PleoraCamera::ProcessFrame(count);


	class_<PythonOriginal, boost::noncopyable >("Original")
		.def("Process", &Original::Process, &PythonOriginal::default_Process)

Then in Python:

class PyOrig(Original):
	def Process(self, count):
		count = count + 10
		return True

orig = PyOrig()

The only thing I did for thread safety was to add the PyGILState calls to the virtual function wrapper around the call<>.  After all that, my question is, is that sufficient?  Also, are there any obvious problems with this example.  It seems to work fine.


More information about the Cplusplus-sig mailing list