[gambit-list] Native threading with C++?

Marc Feeley feeley at iro.umontreal.ca
Thu Mar 22 08:46:56 EDT 2012


On 2012-03-22, at 6:28 AM, Patrick Bene wrote:

> I'm interested in using multithreaded C++ code along with Gambit. From what I
> understand, Gambit does not support native multithreading, but what about
> the FFI? Am I allowed to have multiple C++ threads running alongside Gambit?

Yes you can have multiple OS threads in a process that is executing Gambit compiled code.  Currently, you are constrained to only have a single OS thread at a time executing Gambit compiled code.  The OS thread can change over time, but only one executing Scheme code at a time.  For a simple example, check the directory examples/pthread in the Gambit source distribution.

> Additionally (in case I missed something), is there anyway to get Gambit itself
> running in multiple native threads? I don't need full-blown functionality, but
> I'll take anything that's availible.

That's not currently possible because the Scheme global variables, and some global state of the Gambit VM, are implemented using global C/C++ structures.  What is needed (and something that is on my TODO) is to allow instances of this state to be created dynamically (essentially a constructor for the Gambit VM).  That way several instances of Gambit can coexist in the same OS process.  Unfortunately, this will slow down accesses to global variables, because an indirection will be needed.

If you want to take advantage of parallel execution on a multicore, currently the only approach is to use multiple OS processes.  Each OS process runs an instance of the Gambit program.  They can communicate using the network, a pipe, a fifo, and any other IPC mechanism supported by your OS (see for example http://beej.us/guide/bgipc/output/html/singlepage/bgipc.html).  This is how Termite was implemented (http://code.google.com/p/termite/).

Marc




More information about the Gambit-list mailing list