Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: copy-on-write with MEX files
Date: Wed, 23 Jul 2008 22:25:05 +0000 (UTC)
Organization: Xoran Technologies
Lines: 41
Message-ID: <g68b41$cu8$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1216851905 13256 172.30.248.38 (23 Jul 2008 22:25:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 23 Jul 2008 22:25:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:481362




I'm trying to understand if there is a difference in the 
way copy-on-write is implemented for MEX functions as 
compared to Mfiles.

My understanding is as follows. As part of the copy-on-
write system, MATLAB will pass input data by reference to 
an Mfile function if the function performs read-only 
operations on that data. Otherwise, it will pass the data 
by value. 

However, when it comes to MEX functions, the MATLAB 
interpreter obviously cannot know the contents of the 
function and cannot determine whether the operations it 
will perform are read-only or otherwise. If MATLAB wanted 
to have all the same safety nets as for Mfiles, it would 
have to always pre-duplicate the input data and send that 
to the MEX function instead (effectively passing the data 
by value).

One reason for doing it this way would be to ensure that if 
the MEX file crashes, the input data would remain unaltered 
in the caller workspace. This will be true for Mfiles, and 
the MATLAB software designers may have wanted it to be true 
for MEX files as well.

On the other hand, if data is always passed by value, it 
would obviously reduce the possibilities for computational 
efficiency that people are usually interested in using MEX 
files to obtain.

In any case, I have several colleagues who believe MEX 
files always receive input by value. Can anyone resolve 
this?