[C++-sig] Sharing Global Singleton instances across boost.python extensions (windows & unix)

Joel Gerard llywelyn.geo at yahoo.com
Sat Oct 11 00:01:49 CEST 2003


Thanks for the advice but I don't think I explained my problem very well (and after reading this
post you'll think that to be a gross understatement). I'm not sure its all that off topic but I
suppose it may be a general problem.  The filemapping idea doesn't seem to fit my problem.  Here's
what I'm trying to do:

- I have a library of Math functions that I'm trying to expose to Python: Matrix, Vector etc.

- I also have a library of Geometry types and functions  that I'm trying to expose: Sphere etc.

- Each of these libraries uses functionality in other libraries that I statically link to. In
particular, I have a Mem Manager that's a singleton. So Math and Geometry each statically link to
the MemManager lib in the Boost.Python extension module.

- Once exported using Boost I write python scripts that use them. Additionally, I want to embed
these scripts into a c++ application, which also uses the same libraries in C++ including my
memory manager. 

As I understand it, Boost sets things up to create a Windows DLL that Python explicitly links to
when you use "import" (ie. using LoadLibrary), and then does its magic with the init function etc.
 So what happens for me now is that my app starts the memory manager, fires up python and runs a
script that uses my boost.python extensions, which are supposed to use my Memory Manager.  The
MemoryManager (as well as some other singletons) are supposed to be global in scope so that the
extensions use them.  What's the best way to proceed with Boost so that these globals really are
globals?  I can hack it out in DLL world to make it work, but I also need to port this code to
some kind of POSIXish platform, and I'm not sure if my design will work with SOs.  

First question: What is the best cross platform solution here?

Second question: when you compile the application into which scripts will be embedded and link
against python22.lib is the python library statically linked to my app (ie nothing like
LoadLibrary("python.dll") is going on?)  That is, am I statically linked to the pythoncore, and
not to some thin layer that loads python dynamically?

Third question: can I recompile python and use Boost to statically link my extension modules to
the Python core, and then statically link python to my app and will this solve my problem?

If this is off topic, can you recommend a forum to post this to?

Thanks,
Joel

  

> From:           Niall Douglas <>
 Subject:        Re: [C++-sig] Sharing Singleton instances across boost.python extension modules
> Date sent:      Thu, 09 Oct 2003 01:51:23 +0100
> 
> On 8 Oct 2003 at 16:13, Joel Gerard wrote:
> 
> > How do I share singleton instances across extension modules in
> > Windows?  For instance, if I have an instance of a memory manager in
> > one DLL, how do I use the same memory manager in the other?
> > 
> > I realise this is a big question, but any links, references etc
> > would be appreciated.  Is it even possible?
> 
> Off topic, so off list.
> 
> CreateFileMapping() and repointing the MSVC free store to use a Win32
> heap (HeapAlloc()) allocated within the shared memory region.
> 
> You'll have a nightmare with data locking correctly. I recommend you
> find another solution eg; use a mailslot (CreateMailslot()) to send
> and receive IPC messages between the instances of code.
> 
> Usually when you come to a problem like this, it's one of design. 
> Very rarely will you actually want to do what you're proposing in the 
> long run. We didn't get away from global memory segments on DOS 
> without a very, very good reason.  
> 
> Cheers,
> Niall
> 
> - ------- End of forwarded message -------
> 
> 
> 
> 
> -----BEGIN PGP SIGNATURE-----
> Version: idw's PGP-Frontend 4.9.6.1 / 9-2003 + PGP 8.0.2
> 
> iQA/AwUBP4S2cMEcvDLFGKbPEQKh9wCgvg2h3icoJXZYW2fShQpR9ZK5U64AoOfs
> zk8Xcn5Xjf7q6BRMxYFvrj+W
> =Q/Ut
> -----END PGP SIGNATURE-----


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com




More information about the Cplusplus-sig mailing list