同时从单个Matlab dll调用两个函数 [英] Calling two functions from a single Matlab dll at the same time

查看:409
本文介绍了同时从单个Matlab dll调用两个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含两个功能的Matlab dll. Function1需要3-4毫秒才能运行,Function2大约需要1秒.我需要在C#中连续运行Function1,并不时运行Function2.我遇到的问题是,当我运行Function2时,Function1无法连续运行,或者花费的时间超过3-4毫秒(大约2-3秒). Function2完成后,Function1将返回正常/快速状态.这些是我已经尝试过的:

I have created a Matlab dll that includes two functions. Function1 takes 3-4 millisecond to run and Function2 takes around 1 second. I need to run Function1 in C# continuously and Function2 time to time. I experienced that when I run Function2, Function1 does not run continuously or it takes a lot more than 3-4 millisecond (something in the range of 2-3 second). Function1 returns to normal/fast state as soon as Function2 is completed. These are what I already tried:

  1. 我在单独的线程中调用了Function2,但是没有运气! (Function1也在单独的线程中运行.)
  2. 我用backgroundworker代替了Thread(以防万一),没有运气!
  3. 我仅为Function2创建了一个单独的dll,然后再次遇到相同的问题/延迟/延迟.

有人对这个问题有任何想法/解决方案吗? Matlab是否在单线程中运行函数/代码?如果不是,是否仍然需要为Function指定单独的线程?

Does anyone have any idea/solution for this problem? Does Matlab run functions/code in single thread? If not, is there anyway to specify separate thread for Functions?

感谢您的帮助.

推荐答案

您的直觉似乎是正确的:即使是从多个线程发起的,对Matlab库的调用也会按顺序执行.请参阅Peter Webb在创建C ++共享库和DLL :

It seems like your intuition is correct: Calls to Matlab libraries are executed sequentially, even if originating from multiple threads. Refer to the comments by Peter Webb under Creating C++ Shared Libraries and DLLs:

您可以从多个线程中调用库,但是任何时候库中只能有一个线程处于活动状态.生成的库受到信号量的保护,信号量在任何时候仅允许一个用户线程进入生成的库.尝试调用共享库的其他用户线程将阻塞(等待),直到共享库空闲"为止.

You can call the libraries from multiple threads, but only one thread can be active in a library at any time. The generated libraries are protected by semaphores, which only allow one user thread into the generated library at any one time. Other user threads that try to call into the shared library will block (wait) until the shared library is "free".

[...]

库通过信号量来保护自己.之所以这样做,是因为基础执行引擎(MCR)不是线程安全的.这意味着,即使您可以禁用信号灯,您也不想这样做,因为您可能会得到不正确的结果或程序失败.

The libraries protect themselves with semaphores. They do so because the underlying execution engine (the MCR) is not thread safe. This means that even if you could disable the semaphores, you wouldn’t want to, since you’d likely get incorrect results or program failures.

如果您确实需要并行处理,那么当前最好的(也是唯一的)选择是使用单独的进程.如果您的客户可以说任何标准的Web协议(HTTP或JSON)或Microsoft的专有扩展版本,则使用WCF在单独的过程中设置基于Web的WCF客户端非常简单. (当然,在这种情况下,您的服务器必须在Windows计算机上运行.)请参见

If you truly need parallelism, currently your best (and only) option is to use separate processes. If your client can speak any of the standard web protocols (HTTP or JSON) or Microsoft’s proprietry extended versions, it’s pretty simple to set up web-based WCF clients in separate processes using WCF. (Of course, your servers have to run on Windows machines in that case.) See my WCF post for details.

这篇关于同时从单个Matlab dll调用两个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆