Mutex using 2 variables

huangapple go评论57阅读模式
英文:

Mutex using 2 variables

问题

我正在开发一个由两个独立设备组成的系统,它们通过“共享内存”交换数据。

为了同步它们,我想使用一个内存位置作为“状态变量”或互斥锁。其中,第一个设备会获取互斥锁,而第二个设备会等待它被释放,反之亦然。但现在我发现其中一个设备只能从相同位置读取或写入,不能同时进行两者操作。

所以现在我正试图想出一种使用两个变量来实现互斥锁的方法。一个用于第一个设备写入,第二个设备读取,另一个用于第一个设备读取,第二个设备写入。

这种方法是否不安全?因为我认为这可能会产生大量的读/写循环,可能会引发竞争条件。我一直看到互斥锁只使用一个变量来实现,所以对此的任何指导都将非常有帮助。

英文:

I am working on a system that consists of 2 Independent devices that exchange data through a "common memory".

To sync them, I wanted to use a memory location as a "state variable" or a mutex. Where the first device would take the mutex and the second would wait for it to be released and vice-versa. But now I have discovered that one of those devices can either read OR write from the same location. It cannot do both.

So now I am trying to think of a way to implement a mutex using 2 variables. One where the first device will write and second would read, and the other where the first would read and second would write.

Is this an unsafe approach? Because I think this would create a lot of read/write cycles and possibly race conditions. I've always seen mutexes being implemented with only one variable, so any guidance on this would be very very helpful.

答案1

得分: 1

你正在研发CSMA中的碰撞检测算法,这可能不是你想做的事情,但也许是成功的唯一选择。是否有特定原因你不创建每个设备可写入的内存位置,而另一个设备从该位置读取?这更像是发布-订阅模式,而不是真正的共享内存模型。

英文:

You are inventing the collision detection algorithim in CSMA, probably not what you want to do, but may be your only choice to be successful. Is there a reason you aren't creating a memory location for each device to write to, and have the other device read from that location? More of a pub-sub than a truly shared memory model.

huangapple
  • 本文由 发表于 2023年3月9日 23:48:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定