WaitForSingleObject 没有锁定,仍然允许其他线程更改 C++ 中的值
我正在尝试使用 WaitForSingleObject(fork[leftFork], Infinite);
来使用多个线程锁定变量,但它似乎没有锁定
我设置的 Handle fork[5] 的 任何内容
然后使用下面的代码,但它似乎没有锁定任何东西。
while(forks[rightFork] == 0 || forks[leftFork] == 0) Sleep(0);
WaitForSingleObject(fork[leftFork], INFINITE);
forks[leftFork]--;
WaitForSingleObject(fork[rightFork], INFINITE);
forks[rightFork]--;
我也尝试过使用 WaitForMultipleObjects
并得到相同的结果。当我创建互斥体时,我使用 fork[i]= CreateMutex(NULL, FALSE,NULL);
我想知道这是否只对每个线程都有好处,还是它们共享它?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您没有向我们展示足够的代码,无法帮助您确定正确性。但是,既然提出了这个条件,我还是要尝试一下!
您对“fork”一词的使用表明您正在从 pthreads 背景接近 Windows 线程。 Windows 线程略有不同。令人困惑的是,Windows 中最有效的进程内互斥对象不是互斥体,它实际上是临界区。
关键部分的界面非常多使用起来更简单,它本质上是一个获取函数和相应的释放函数。如果您在单个进程内进行同步,并且需要一个简单的锁(而不是信号量),则应该使用临界区而不是互斥体。
事实上,就在昨天,我在 Stack Overflow 上为一个问题写了一个更详细的答案,该问题描述了关键部分的标准使用模式。该帖子有很多 MSDN 文档相关部分的链接。
话虽如此,您似乎要做的就是同步整数值数组的递减。如果是这样,那么您可以使用
InterlockIncrement
或其朋友之一。仅当执行跨进程同步时才需要使用互斥锁。事实上,您应该只在跨进程同步时使用互斥体,因为关键部分的性能要好得多(即更快)。由于您在这里更新一个简单的数组,并且没有明显可见的 IPC 正在进行,因此我只能得出结论,这确实是在进行中。
如果我错了,并且您确实正在执行跨进程工作并且需要互斥锁,那么我们将需要查看更多代码。例如,我没有看到任何对
ReleaseMutex
的调用。我不知道你是如何创建互斥体的。如果这没有帮助,请编辑您的问题以包含更多代码,以及您想要实现的目标的高级概述。
First of all, you haven't shown enough code for us to be able to help you with any great certainty of correctness. But, having made that proviso, I'm going to try anyway!
Your use of the word fork suggests to me that you are approaching Windows threading from a pthreads background. Windows threads are a little different. Rather confusingly, the most effective in-process mutex object in Windows is not the mutex, it is in fact the critical section.
The interface for the critical section is much simpler to use, it being essentially an acquire function and a corresponding release function. If you are synchronizing within a single process, and you need a simple lock (rather than, say, a semaphore), you should use critical sections rather than mutexes.
In fact, only yesterday here on Stack Overflow, I wrote a more detailed answer to a question which described the standard usage pattern for critical sections. That post has lots of links to the pertinent sections of MSDN documentation.
Having said that, it would appear that all you are trying to do is to synchronize the decrementing of an array of integer values. If that is so then you can do this most simply in a lock free manner with
InterlockIncrement
or one of its friends.You only need to use a mutex when you are performing cross process synchronization. Indeed you should only use a mutex when you are synchronizing across a process because critical sections perform so much better (i.e. faster). Since you are updating a simple array here, and since there is no obviously visible IPC going on, I can only conclude that this really is in-process.
If I'm wrong and you really are doing cross-process work and require a mutex then we would need to see more code. For example I don't see any calls to
ReleaseMutex
. I don't know exactly how you are creating your mutexes.If this doesn't help, please edit your question to include more code, and also a high level overview of what you are trying to achieve.