[C++-sig] Deriving Python function from C++ base class with pure virtual function

Niall Douglas s_sourceforge at nedprod.com
Sun Nov 14 18:09:57 CET 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 13 Nov 2004 at 1:53, Paul F. Kunz wrote:

>    However, I want to also call operator()(double) from C++ code in a
> different thread from the Python main thread.   This other thread is
> started with Qt's QThread class.   When I do this I get to
> FunctionWrap::operator()(double) and then seg fault with the following
> back trace in gdb...
> [snip]
>    Apparently I have threading problems but don't know how to
> procede.   I tried wrapping call_method with
> Py_ALLOW_THREADS_BEGIN/END, but that seg faults immediately.   
> 
>    Should I start the second thread with Python's own thread class? Is
> there some Python C API functions I could call to read the Python
> object to be called from this second thread?   Any suggestions /
> advise would be appreciated.

Before entering python, you must ensure that python's thread state 
has been set for the current thread. This implies holding python's 
Global Interpreter Lock. If you try entering python without setting 
the thread state, you'll get a warning on stderr and a segfault most 
probably.

If however you want to have multiple threads working with python 
concurrently, that is a whole new bag of worms. There is a solution 
but it involves patching BPL to release and grab the python GIL every 
time it enters and exits C/C++ code. Do say if you want to do this, 
but be warned it's not trivial.

Cheers,
Niall





-----BEGIN PGP SIGNATURE-----
Version: idw's PGP-Frontend 4.9.6.1 / 9-2003 + PGP 8.0.2

iQA/AwUBQZeRZcEcvDLFGKbPEQIcHwCg1hpD6OKp34ea0ow2GsU1CMg7uiUAoO5a
fk2O1bpfwGhTGv6/m9h9ATd0
=qgYg
-----END PGP SIGNATURE-----



More information about the Cplusplus-sig mailing list