Added lock to mex file to prevent problems when clearing