MATLAB 同步代码

标签 matlab synchronized

Java 在 MATLAB 中的“同步”是什么?

假设我有两个计时器,它们都可以修改一个变量(即矩阵)M。如果它们同时触发,它们是否会同时尝试更改 M(这可能会导致错误)? MATLAB 会自动同步这些操作吗?

最佳答案

Matlab 99% 是单线程的(另外 1% 与这个问题无关)。因此,没有可用的 synchronized 关键字。

但是,有些操作是可中断的,这意味着 GUI 回调或定时器可以在意外的时间暂停操作。这可能会导致一些在多线程系统中可以观察到的相同问题。

要防止中断,请在可用时使用 interruptible 属性(通常在 GUI 对象上)。这应该可以处理在处理 GUI 回调时防止重入行为的需要。例如

set(gcf,'Interruptible','off')

但是,这不会处理与计时器相关的中断。


看来两个定时器不能互相中断,所以不需要同步。但是,计时器可以中断主要事件。

经过一些测试,这可能发生在 pausedrawnowfiguregetframe 调用附近,这在文档中暗示。它也可能发生在其他调用附近,包括一些 tic/toc 调用和对 Java 的调用。

我不知道定时器或函数的 Interruptible 属性是否有并行性,尽管它可能是需要的。根对象 0 具有 Interruptible 属性,但它没有任何效果(根据文档,并已确认)。

注意:与我之前提供的答案(参见历史记录)相比,这是一个很大的变化,代表了最近的学习。我之前的示例使用了两个计时器,它们似乎相互消除了冲突。本例使用一个定时器加main函数操作。


下面包含一个示例,演示一种不可中断的情况,以及函数 some_work 被中断的两种情况。

function minimum_synchronization_example

%Defune functions to test for interruptability
%(these are all defined at the bottom of the file)
fnList = {
    @fn_expectedNoInterruption
    @fn_expectedInterruption
    @fn_unexpectedInterruption
    };
for ix = 1:length(fnList)
    disp(['---Examples using ' func2str(fnList{ix}) '--------'])
    test_subfunction_for_interrupt_allowed(fnList{ix});
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function test_subfunction_for_interrupt_allowed(fn)
%Setup and start a timer to execute "some_work"
t1 = timer();
set(t1,...
    'Name','PerformSomeWorkTimer1', ...
    'TimerFcn', @(~,~) some_work('Timer-1', fn), ...
    'ExecutionMode','fixedSpacing', ...
    'Period', 0.4, ...
    'TasksToExecute', 6, ...
    'BusyMode', 'drop')
start(t1);

%Then immediately execute "some_work" from the main function
for ix = 1:6
    some_work('MainFun', fn);
    pause(0.2);
end

%The timer and the loop take about the same amount of time, stop and delete
%the timer before moving on
stop(t1);
delete(t1);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function some_work(displayString, subFunctionWhichMayAllowInterrupts)
%Initialize persistent, if needed.
%This records then umber of active calls to this function.
persistent entryCount
if isempty(entryCount)
    entryCount = 0;
end

%Record entry
entryCount = entryCount+1;
tic;

%Perform some work  (Inverting a 3000-by-3000 matrix takes about 1 sec on my computer)
[~] = inv(randn(3000));

%Run subfunction to see if it will be interrupted
subFunctionWhichMayAllowInterrupts();

%Display results. How many instances of this function are currently active?
if entryCount>1;
    strWarning = 'XXX ';
else
    strWarning = '    ';
end
disp([strWarning ' <' sprintf('%d',entryCount) '> [' displayString '] ; Duration: ' sprintf('%7.3f',toc)]);

%Record exit
entryCount = entryCount-1;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function fn_expectedNoInterruption
x = 1+1;
end

function fn_expectedInterruption
drawnow;
end

function fn_unexpectedInterruption
m = java.util.HashMap();
end

关于MATLAB 同步代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15693617/

相关文章:

java - 为什么需要以毫秒为单位的超时以及为什么我必须为单独的 block 声明两个具有单独 run () 方法的类

matlab - 使用 MATLAB 在 x 上对函数 f(x) 进行数值积分,其中 f(x) 具有另一个参数 y,它是一个向量

java - 一个同步块(synchronized block)与多个 AtomicInteger 增量相比

java - 如何单独同步数据成员

matlab - 检查天气预报值是否遵循高斯分布或不使用 matlab?

java - 同步会影响对象成员吗?

java - "static"字段的延迟初始化应该是 "synchronized"- 如何修复它

matlab - 图例被截断 MATLAB

在Matlab中直接从url读取csv数据

arrays - 根据重复索引对数组求和