Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread safety of Matlab engine API

I have discovered through trial and error that the MATLAB engine function is not completely thread safe.

Does anyone know the rules?

Discovered through trial and error:

On Windows, the connection to MATLAB is via COM, so the COM Apartment threading rules apply. All calls must occur in the same thread, but multiple connections can occur in multiple threads as long as each connection is isolated.

From the answers below, it seems that this is not the case on UNIX, where calls can be made from multiple threads as long as the calls are made serially.

like image 959
Jeremy Avatar asked Oct 29 '08 21:10

Jeremy


People also ask

What is Threadsafe?

Thread safe: Implementation is guaranteed to be free of race conditions when accessed by multiple threads simultaneously. Conditionally safe: Different threads can access different objects simultaneously, and access to shared data is protected from race conditions.

Which object is thread-safe?

An immutable object is one whose state can't be changed once the object is created. Immutable objects are, by their very nature, thread-safe simply because threads have to be able to write to an object's instance variables to experience a read/write or write/write conflict.

Are threads protected from each other?

Since every thread can access every memory address within the process' address space, one thread can read, write, or even completely wipe out another thread's stack. There is no protection between threads because (1) it is impossible, and (2) it should not be necessary.


1 Answers

From the documentation,

MATLAB libraries are not thread-safe. If you create multithreaded applications, make sure only one thread accesses the engine application.

like image 80
Jacob Avatar answered Sep 30 '22 19:09

Jacob