cpf
cpf

Reputation: 1501

boost python with threads

It seems boost::python and boost::thread don't really like each other for what I can tell.

Please refer to http://pastebin.com/Cy123mJK

This is a simplification of the problems I am having with my boost::python and boost::thread-based application.

If anyone can tell me why these problems are occurring; I have no clue, since I strictly make sure python interaction is done with one thread at once.

At some point, the program crashes with a segfault for no obvious reason. Also, it's impossible to catch this crash it seems...

Help much appreciated!

Upvotes: 4

Views: 1667

Answers (1)

Tonttu
Tonttu

Reputation: 1821

You are running python in multiple threads at the same time in Producer::run() and Consumer::run().

To be exact, you run this before locking the mutex:

boost::python::object writer = this->k->Get<boost::python::object>("write");

Maybe you didn't realize that Boost eventually calls PyObject_GetItem when you call boost::python::object::operator[](const std::string&) in Keeper::Get. You need to move that Get-call to the correct location, after locking and before using the returned function:

{
  boost::mutex::scoped_lock l(this->k->python_keeper);
  boost::python::object writer = this->k->Get<boost::python::object>("write");
  writer(boost::python::str(os.str()));
}

Edit: Removed Py_Finalize(). Yes you are right, boost.python doesn't like it.

Upvotes: 7

Related Questions