Services & Resources / Wolfram Forums / MathGroup Archive

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Multiple threads using Mathlink

  • To: mathgroup at
  • Subject: [mg88808] Re: [mg88775] Multiple threads using Mathlink
  • From: Steve Wilson <stevew at>
  • Date: Sat, 17 May 2008 05:29:54 -0400 (EDT)
  • References: <>


Generally speaking it is safe to use MathLink with threads.   Here are 
the relevant stipulations:

1.  Do not concurrently access the same MLENV object from more than 
one thread at a time.   This means that you do not want multiple 
threads invoking any of the MLOpen* functions using the same MLENV 
object simultaneously.  You will need to protect access to the MLENV 
object with a thread locking mechanism.

2.  Do not concurrently access the same MLINK object from more than 
one thread at a time.   You will need to protect access to the MLINK 
object with a thread locking mechanism.

To answer your question about MathLink and Kernels, you should 
understand  the following about MathLink.   MathLink does not 
specifically create Kernel processes.   MathLink creates a 
communication link between one process and another process.   MathLink 
supports the ability to start the other process, but fundamentally 
what you are dealing with is a full duplex communication link between 
two processes.   MathLink links function in a one-to-one paradigm, 
thus one link can start and communicate with one process.

Senior Network/System Protocol Developer
Wolfram Research Inc.

On May 16, 2008, at 3:32 AM, Pawe=C5=82 Banasik wrote:

> Hello,
> I have a question regarding multithreaded access to Mathematica via 
> MathLink.
> I'm developing a set of webservices for use with the Taverna Workbench
> (, a computational biology tool. These 
> webservices
> call Mathematica to perform some computations. Obviously, each 
> webservice
> invocation creates a new thread. As I'd like the Mathematica 
> environments
> belonging to different threads to be completely separated one from 
> another,
> I've implemented a mechanism which creates a new MathLink object for 
> every
> thread. Unfortunately, this leads to serious problems, including an
> application service crash, occuring from time to time. Apparently, 
> this is
> some kind of a concurrent access problem - it doesn't happen when I 
> run only
> one thread at a time, with subsequent invocations separated by a few 
> seconds.
> Perhaps there is a problem with my understanding of the relation 
> between
> MathLink and Mathematica kernels? Is it true that each Mathlink object
> creates a separate kernel process? If not, how to achieve such 
> behavior? Do
> the MathLink native libraries support multithreading?
> Thank you for your help,
> Regards,
> Pawel Banasik

  • Prev by Date: Re: Re: Replace non-numeric values in a list
  • Next by Date: Re: Traditional Ordering of Equations
  • Previous by thread: Multiple threads using Mathlink
  • Next by thread: Re: Re: Multiple threads using Mathlink