windows 对 CreateMutex 的困惑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5115900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Confusion on CreateMutex
提问by Jake
Let says I call CreateMutex.
假设我调用 CreateMutex。
HANDLE h;
h=CreateMutex(NULL, TRUE, NULL);
waitforsingleobject(h, INFINITE);
////Random Code
ReleaseMutex(h);
Assuming I have multiple threads running, the first thread to reach the function createmutex essentially blocks all the other threads from the //random code section until release mutex is called right?
假设我有多个线程在运行,第一个到达函数 createmutex 的线程实际上会阻塞 //random 代码部分中的所有其他线程,直到调用释放互斥锁,对吗?
回答by decltype
It doesn't, because you have created an unnamed mutex (the third parameter is the name). Assuming the example code is run in multiple threads, each thread will create a new unnamed mutex, and will promptly get access to the critical section(Random Code), because they are only waiting for their own mutex.
它没有,因为您创建了一个未命名的互斥锁(第三个参数是名称)。假设示例代码在多个线程中运行,每个线程都会创建一个新的未命名的互斥锁,并会及时访问临界区(随机代码),因为它们只是在等待自己的互斥锁。
To fix this, either let h be a global handle that all threads have access to, and call CreateMutex once outside the shared code, or provide CreateMutex with a name (third argument). In the latter case, subsequent calls to CreateMutex will return a handle to the existing mutex.
要解决这个问题,要么让 h 成为所有线程都可以访问的全局句柄,并在共享代码之外调用一次 CreateMutex,要么为 CreateMutex 提供一个名称(第三个参数)。在后一种情况下,对 CreateMutex 的后续调用将返回现有互斥锁的句柄。
回答by ds27680
Well taking into account that you create an unnamed mutex, each thread will create an unnamed mutex of its own and assume ownership upon creation. Since each thread has its own mutex all threads will be able to run in parallel.
考虑到您创建了一个未命名的互斥锁,每个线程将创建一个自己的未命名的互斥锁并在创建时承担所有权。由于每个线程都有自己的互斥锁,因此所有线程都可以并行运行。
So you should create the mutex once for all threads if you want it to be unnamed. From looking at your code you should also check if mutex creation succeeded.
因此,如果您希望它未命名,您应该为所有线程创建一次互斥锁。通过查看您的代码,您还应该检查互斥锁创建是否成功。
Then in the function that gets called from multiple threads call WaitForSingleObject on the previously created mutex.
然后在从多个线程调用的函数中,对先前创建的互斥锁调用 WaitForSingleObject。
Check the returned result if it returned because you got ownersip of the mutex or the wait terminanted because the mutex was abandoned.
检查返回的结果,如果它返回是因为您获得了互斥锁的 ownerip 或由于互斥锁被放弃而等待终止。
Make sure you call ReleaseMutex on the mutex handle if you got ownersip. Probably you should think of using RAII to manage the mutex for many reasons (to make sure the mutex is properly released when exceptions occur or when a return statement is inserted before the ReleaseMutex to name just a few).
如果您有 ownerip,请确保在互斥锁句柄上调用 ReleaseMutex。出于多种原因,您可能应该考虑使用 RAII 来管理互斥锁(以确保在发生异常时或在 ReleaseMutex 之前插入 return 语句时正确释放互斥锁,仅举几例)。
Sometime when the mutex is no longer needed make sure you call CloseHandle on the MutexHandle.
有时不再需要互斥锁时,请确保在 MutexHandle 上调用 CloseHandle。
For an example illustrating how to use mutexes see here: "Using Mutex Objects"
有关说明如何使用互斥锁的示例,请参见此处:“使用互斥锁对象”
For a basic multithreading wrap see here: Using Threads
有关基本的多线程包装,请参见此处:使用线程
回答by sharptooth
Since you don't specify a name for the mutex each thread will create its own distinct mutex other threads will not be aware of.
由于您没有为互斥锁指定名称,因此每个线程将创建自己独特的互斥锁,其他线程将不知道。
A critical section would be a better choice in your scenario.
在您的场景中,临界区将是更好的选择。
回答by OJ.
Only if the same mutex reference is shared across all threads/processes. You're not sharing it, you're creating it each time. You'll need to name it so that they all get the same mutex for your code to work.
仅当所有线程/进程共享相同的互斥引用时。你不是在分享它,你每次都是在创造它。您需要对其进行命名,以便它们都获得相同的互斥锁,以便您的代码正常工作。