Py_Initialize(); // Python PyEval_InitThreads(); // Python GIL mGilState = PyGILState_Ensure(); // GIL mThreadState = PyEval_SaveThread(); // GIL // GIL Python
// GIL PyEval_RestoreThread( mThreadState ); // GIL PyGILState_Release( mGilState ); // GIL Py_Finalize(); // , - Python
class PyMainThread // { public: PyMainThread() // { Py_Initialize(); // Python PyEval_InitThreads(); // Python GIL mGilState = PyGILState_Ensure(); // GIL mThreadState = PyEval_SaveThread(); // GIL // GIL Python } ~PyMainThread() // { // GIL PyEval_RestoreThread( mThreadState ); // GIL PyGILState_Release( mGilState ); // GIL Py_Finalize(); // , - Python } private: PyGILState_STATE mGilState; // GIL PyThreadState* mThreadState; // };
PyMainThread main_thread; // boost::thread_group group; // , , Python GIL for( int id = 1; id <= THREAD_NUM; ++id) group.create_thread( ThreadWork(id) ); group.join_all();
mMainGilState = PyGILState_Ensure(); // mOldThreadState = PyThreadState_Get(); // mNewThreadState = Py_NewInterpreter(); // - PyThreadState_Swap( mNewThreadState ); // - mSubThreadState = PyEval_SaveThread(); // GIL mSubGilState = PyGILState_Ensure(); // GIL -
PyGILState_Release( mSubGilState ); // GIL - PyEval_RestoreThread( mSubThreadState ); // Py_EndInterpreter( mNewThreadState ); // - PyThreadState_Swap( mOldThreadState ); // PyGILState_Release( mMainGilState ); // GIL
class PySubThread // { public: PySubThread() // - Python { mMainGilState = PyGILState_Ensure(); // mOldThreadState = PyThreadState_Get(); // mNewThreadState = Py_NewInterpreter(); // - PyThreadState_Swap( mNewThreadState ); // - mSubThreadState = PyEval_SaveThread(); // GIL mSubGilState = PyGILState_Ensure(); // GIL - } ~PySubThread() // - Python { PyGILState_Release( mSubGilState ); // GIL - PyEval_RestoreThread( mSubThreadState ); // Py_EndInterpreter( mNewThreadState ); // - PyThreadState_Swap( mOldThreadState ); // PyGILState_Release( mMainGilState ); // GIL } private: PyGILState_STATE mMainGilState; // GIL Python PyThreadState* mOldThreadState; // PyThreadState* mNewThreadState; // - PyThreadState* mSubThreadState; // GIL PyGILState_STATE mSubGilState; // GIL - Python };
#include <boost/python.hpp> #include <boost/thread.hpp> using namespace boost::python; using namespace boost::this_thread; using namespace boost::posix_time; void wait( double sec ) // , time.sleep Python { int msec = static_cast<int>( sec * 1000 ); // sleep( millisec( msec ) ); // } BOOST_PYTHON_MODULE( ctimer ) // boost::python ctimer { def( "wait", wait, args("sec") ); // Python ctimer.wait(sec) }
class ThreadWork // - boost::thread { public: ThreadWork( int id ) // : mID( id ) { } void operator () ( void ) // { cout << "Thread#" << mID << " <= START" << endl; PySubThread sub_thread; // - Python for( int rep = 1; rep <= REPEAT_TIMES; ++rep ) { // Python cout << "Thread#" << mID << " <= Repeat#" << rep << " <= import time; time.sleep(pause)" << endl; object time = import( "time" ); // import time time.attr( "sleep" )( PAUSE_SEC ); // time.sleep(pause) // C++ cout << "Thread#" << mID << " <= Repeat#" << rep << " <= import ctimer; ctimer.wait(pause)" << endl; object ctimer = import( "ctimer" ); // import ctimer ctimer.attr( "wait" )( PAUSE_SEC ); // ctimer.wait(pause) } cout << "Thread#" << mID << " <= END" << endl; // - Python } private: int mID; // };
Source: https://habr.com/ru/post/167261/
All Articles