名为Mutex的单声道替代品 [英] Mono alternative for named Mutex

查看:93
本文介绍了名为Mutex的单声道替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows/.NET上,可以使用命名的Mutex来同步多个进程.不幸的是,Mono在Linux上并不完全支持此功能.他们的发行说明表示Linux不支持此Windows功能,因此无法对其进行仿真.无论如何,似乎最好避免提议的骇客启用它.

On Windows/.NET, a named Mutex can be used to synchronise multiple processes. Unfortunately, Mono doesn't quite support this on Linux. Their release notes say that Linux doesn't support this Windows feature and it would be unreliable to emulate it. It seems best to avoid the proposed hack to enable it anyway.

那么建议的替代方法是什么?我需要确保程序可以安全地并发运行,只有一小部分需要与其他实例同步.

So what are suggested alternatives? I need to make my program safe to run concurrently, only a short section of it needs to be synchronised with other instances.

该应用程序最终需要部署在具有Mono 2.10的Ubuntu Linux上,但是对于测试,如果它也可以在具有.NET 4的Windows 7上运行,将不胜感激.

The application eventually needs to be deployed on Ubuntu Linux with Mono 2.10, but for testing, it would be highly appreciated if it also works on Windows 7 with .NET 4.

推荐答案

更新:
尝试查看 http://aakinshin.net/en/blog/dotnet /namedmutex-on-mono/

UPDATE:
Try to look at http://aakinshin.net/en/blog/dotnet/namedmutex-on-mono/

OLD:
Mono不支持任何Windows本地IPC. 因此,您没有例如命名管道或互斥对象.

OLD:
Mono does not support any Windows-native IPC. So you do not have for example Named Pipes, or Mutexes.

但是要在同一过程中同步线程,可以使用

But to sync threads in the same process you can use Monitor class explicitly (it also used for lock).

要简单地通知另一个进程,您可以尝试使用 Unix域套接字.
为此,检查 UnixEndPoint 类.您可以为其指定名称的好处之一(例如,为命名信号量).

To simply notify another process you can try to use Unix Domain Sockets.
Check UnixEndPoint class for that. One of benefits you can specify name for it (like for named semaphore for example).

您还可以尝试使用自己的文件模拟Mutex .尝试获得对特定自己文件的独占访问权.拥有访问权限后,您将进入关键区域.

Also you can try to emulate Mutex using own file. Try to get exclusive access to specific own file. While you have that access - you are in critical section.

这篇关于名为Mutex的单声道替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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