Python thread memory layout (in combination with boost::python)

83 views Asked by At

I have a boost::python application written in C++. This code is compiled into a binary that also includes the Python interpreter. The binary is then called with a Python script that imports the C++ module:

./c++executable script.py

Now I would like to parallelize the code using Python threads: In the Python code I want to create threads which then (among other things) call functions written in C++.

I cannot, however, find information about the memory layout used by the python threads:

  • Will each thread have its own defined memory section to use or will different threads try to allocate memory in the same memory section?

  • Provided each thread gets its own (deep copies of) C++ objects, will there be any interference between the threads?

This runs on a Linux OS. The application is compiled with the -lpthread flag, if that makes a difference.

I would be grateful if anyone can shed some light on these questions.

1

There are 1 answers

0
David Schwartz On

Will each thread have its own defined memory section to use or will different threads try to allocate memory in the same memory section?

The whole point of threads is to share all memory.

Provided each thread gets its own (deep copies of) C++ objects, will there be any interference between the threads?

That's your responsibility as the programmer -- to ensure that there isn't. Where threads are going to be working on the same data, and particularly where one thread might access something while another thread is modifying it, you have to use proper synchronization.

A piece of advice: If you're not familiar with threading in both C++ and Python, learn each of those first, before you try to use both languages, and threads, together.