4

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!

1 Answer 1

7
+200

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for the answer. I have incorporated your suggestion in the test program, it's running at the moment. Also, boost::python documentation claims Py_Finalize() shouldn't be called: ur1.ca/2zuwb
The test program has been running since my last comment without interruption. I thank you for spotting my thinking error!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.