Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MATLAB同步代码_Matlab_Synchronized - Fatal编程技术网

MATLAB同步代码

MATLAB同步代码,matlab,synchronized,Matlab,Synchronized,Java在MATLAB中的“同步”等价于什么 假设我有两个计时器,它们都可以修改一个变量(即矩阵)M。如果它们同时触发,它们是否会同时尝试更改M(这可能会导致错误)?MATLAB会自动同步这些操作吗?MATLAB 99%是单线程的(另外1%与此问题无关)。因此,没有可用的synchronized关键字 然而,有些操作是可中断的,这意味着GUI回调或计时器可以在意外的时间暂停操作。这可能会导致一些在多功能系统中可以观察到的问题 要防止中断,请在可用时使用interruptable属性(通常在GUI

Java在MATLAB中的“同步”等价于什么


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

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

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

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

set(gcf,'Interruptible','off')
但是,这不处理与计时器相关的中断


似乎两个计时器不能相互中断,因此不需要同步。但是,计时器可以中断主活动

在一些测试之后,这可能发生在文档中暗示的
暂停
绘图
图形
获取帧
调用附近。它也可以发生在其他调用附近,包括一些tic/toc调用和对Java的调用

我不知道计时器或函数的
interruptable
属性的并行性,即使可能需要。根对象
0
有一个
interruptable
属性,但它没有任何效果(根据文档,并已确认)

注意:这与我之前提供的答案(参见历史)相比有很大的变化,代表了最近的学习。我前面的示例使用了两个计时器,它们似乎彼此不冲突。此示例使用一个计时器加上主功能操作


下面是一个示例,演示了一种不可中断的情况,以及两种功能
某些工作被中断的情况

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
函数最小值\u同步\u示例
%用于测试可中断性的去调谐功能
%(这些都在文件底部定义)
fnList={
@fn_预计不会中断
@fn_预期的中断
@意外中断
};
对于ix=1:长度(fnList)
disp(['--使用'func2str(fnList{ix}')的示例-----']
测试子功能是否允许中断(fnList{ix});
结束
结束
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
允许中断的功能测试子功能(fn)
%设置并启动计时器以执行“某些工作”
t1=定时器();
集合(t1,。。。
'Name'、'PerformSomeWorkTimer1'、。。。
“TimerFcn”,@(~,~)一些工作('Timer-1',fn)。。。
'ExecutionMode'、'fixedSpacing'、。。。
“期间”,0.4。。。
'TasksToExecute',6。。。
‘BusyMode’、‘drop’)
启动(t1);
%然后立即从主函数执行“一些工作”
对于ix=1:6
一些工作(“主要乐趣”,fn);
暂停(0.2);
结束
%计时器和循环占用的时间大致相同,停止和删除
%继续之前,请先关闭计时器
停止(t1);
删除(t1);
结束
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
函数部分工作(显示字符串,子函数,可能会中断)
%如果需要,初始化persistent。
%然后记录对该函数的活动调用数。
持续入口计数
如果为空(entryCount)
入口计数=0;
结束
%记录条目
entryCount=entryCount+1;
抽搐;
%执行一些工作(在我的计算机上反转3000×3000矩阵大约需要1秒)
[~]=inv(兰登(3000));
%运行子函数以查看它是否会被中断
子函数,该子函数可能会中断();
%显示结果。此函数当前有多少个实例处于活动状态?
如果入口计数>1;
strWarning='XXX';
其他的
strWarning='';
结束
disp([strWarning'['displayString'];Duration:'sprintf('%7.3f',toc)]);
%记录出口
entryCount=entryCount-1;
结束
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
功能fn_预计不会中断
x=1+1;
结束
功能fn_预期中断
现在抽;
结束
功能fn_意外中断
m=java.util.HashMap();
结束

可能值得注意的是,如果在计时器回调中调用
drawnow
drawnow update
,MATLAB可以从计时器回调中处理事件队列的其余部分(即调用另一个计时器的回调)。然而,这仍然是单线程行为。我不得不说这是一个可怕的限制。无法中断昂贵但优先级较低的操作(即绘图),以进行快速但优先级较高的操作(实时控制)。