C# 我可以用C创建优先同步锁吗#

C# 我可以用C创建优先同步锁吗#,c#,.net,synchronization,locking,ipc,C#,.net,Synchronization,Locking,Ipc,我目前正在使用在进程之间建立对文件的同步访问,例如: //Process 1 //High frequency "writes" try { mutex.WaitOne(System.Threading.Timeout.Infinite); try { //Do write operation } finally { mutex.ReleaseMutex(); } } catch(AbandonedMut

我目前正在使用在进程之间建立对文件的同步访问,例如:

//Process 1
//High frequency "writes"
try
{
    mutex.WaitOne(System.Threading.Timeout.Infinite);

    try
    {
        //Do write operation

    }
    finally
    {
        mutex.ReleaseMutex();
    }
}
catch(AbandonedMutexException)
{
    //Log error
}
有时我可能需要检查写入文件的内容:

//Process 2
//Low frequency "reads"
try
{
    mutex.WaitOne(System.Threading.Timeout.Infinite);

    try
    {
        //Do read operation

    }
    finally
    {
        mutex.ReleaseMutex();
    }
}
catch(AbandonedMutexException)
{
    //Log error
}
但是使用这种技术所发生的情况是,正在进行低频“读取”的
进程2
似乎挂起并且从未接收到对资源的访问,或者可能需要很长时间才能完成

在我的情况下有更好的锁吗


另外,它必须兼容才能在进程之间使用。

您是否使用这样的命名互斥体?:

bool created = false;       
string mutexHandle = "SOME_UNIQUE_ENOUGH_STRING_slkuhfwer7487rctcwf6gt86efcwwgsa";
var myFileLock = new Mutex(true, mutexHandle, out created);
因为只有一个命名的互斥体可以用于在系统级的进程之间同步任务

如果是这样,你做的是正确的事情(技术上)


但如果您在一端执行高频操作;您应该提供像
Thread.Sleep(1)这样的间隙
before
mutex.WaitOne(System.Threading.Timeout.Infinite)(例如);或者,您应该使用另一种方法,如内存映射文件,或者写入新的标记文件(通过在名称中提供时间戳并删除读取的文件),或者使用为您的文件提供版本控制的工具。

是否使用这样的命名互斥体?:

bool created = false;       
string mutexHandle = "SOME_UNIQUE_ENOUGH_STRING_slkuhfwer7487rctcwf6gt86efcwwgsa";
var myFileLock = new Mutex(true, mutexHandle, out created);
因为只有一个命名的互斥体可以用于在系统级的进程之间同步任务

如果是这样,你做的是正确的事情(技术上)


但如果您在一端执行高频操作;您应该提供像
Thread.Sleep(1)这样的间隙
before
mutex.WaitOne(System.Threading.Timeout.Infinite)(例如);或者,您应该使用另一种方法,如内存映射文件,或者写入新的标记文件(通过在名称中提供时间戳并删除读取的文件),或者使用为您的文件提供版本控制的工具。

我想我通过引入一个事件修复了它:

EventWaitHandle event = new EventWaitHandle(true, 
     EventResetMode.ManualReset, 
     strEventGlobalName,     //Must be the same for each process
     out dummy, 
     eventSecurity);         //To allow access between processes
因此,作者看起来是这样的:

//Process 1
//High frequency "writes"

//Wait to allow writing
if (event.WaitOne(System.Threading.Timeout.Infinite))
{
    try
    {
        mutex.WaitOne(System.Threading.Timeout.Infinite);

        try
        {
            //Do write operation

        }
        finally
        {
            mutex.ReleaseMutex();
        }
    }
    catch(AbandonedMutexException)
    {
        //Log error
    }
}
以及读者本人:

//Process 2
//Low frequency "reads"

try
{
    //Reset event to halt writing
    if (!event.Reset())
        throw new Exception("Did not reset event");

    try
    {
        mutex.WaitOne(System.Threading.Timeout.Infinite);

        try
        {
            //Do read operation

        }
        finally
        {
            mutex.ReleaseMutex();
        }
    }
    catch(AbandonedMutexException)
    {
        //Log error
    }
}
finally
{
    //Set event to allow back writing
    if(!event.Set())
        throw new Exception("Did not set event");
}

我想我通过介绍一个活动解决了这个问题:

EventWaitHandle event = new EventWaitHandle(true, 
     EventResetMode.ManualReset, 
     strEventGlobalName,     //Must be the same for each process
     out dummy, 
     eventSecurity);         //To allow access between processes
因此,作者看起来是这样的:

//Process 1
//High frequency "writes"

//Wait to allow writing
if (event.WaitOne(System.Threading.Timeout.Infinite))
{
    try
    {
        mutex.WaitOne(System.Threading.Timeout.Infinite);

        try
        {
            //Do write operation

        }
        finally
        {
            mutex.ReleaseMutex();
        }
    }
    catch(AbandonedMutexException)
    {
        //Log error
    }
}
以及读者本人:

//Process 2
//Low frequency "reads"

try
{
    //Reset event to halt writing
    if (!event.Reset())
        throw new Exception("Did not reset event");

    try
    {
        mutex.WaitOne(System.Threading.Timeout.Infinite);

        try
        {
            //Do read operation

        }
        finally
        {
            mutex.ReleaseMutex();
        }
    }
    catch(AbandonedMutexException)
    {
        //Log error
    }
}
finally
{
    //Set event to allow back writing
    if(!event.Set())
        throw new Exception("Did not set event");
}

不,这是一个糟糕的方法。没有必要“放慢”一个线程/进程的运行速度。我并没有说这是一个糟糕的方法。由于我们在系统级没有类似“ReaderWriterLock”的东西(顺便说一句,ReaderWriterLock支持写操作而不是读操作);通过添加“慢下来”,我们为读者提供了一个机会。而且这不全是关于锁定。也许每次写入时应该向文件中写入更多的数据,而不是多次写入许多微小的数据。如果我们真的想深入优化它,那么我们应该为数据库编写一个(微小的)持久层;例如,考虑硬盘的“页面大小”之类的因素:)不,这是一个糟糕的方法。没有必要“放慢”一个线程/进程的运行速度。我并没有说这是一个糟糕的方法。由于我们在系统级没有类似“ReaderWriterLock”的东西(顺便说一句,ReaderWriterLock支持写操作而不是读操作);通过添加“慢下来”,我们为读者提供了一个机会。而且这不全是关于锁定。也许每次写入时应该向文件中写入更多的数据,而不是多次写入许多微小的数据。如果我们真的想深入优化它,那么我们应该为数据库编写一个(微小的)持久层;例如,考虑硬盘的“页面大小”之类的因素:)