windows windows中的互斥锁问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7545501/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:08:58  来源:igfitidea点击:

mutex problem in windows

c++cwindowsmutex

提问by pooya

I have problem with mutexes I have this code and I dont any idea why it doesn't work correctly...

我有互斥体的问题我有这个代码,我不知道为什么它不能正常工作......

#include <windows.h>
#include <process.h>
#include <stdio.h>
HANDLE mutex;
unsigned _stdcall t(void*){
printf(":D:D:D\n");
return NULL;
}
int main(){
mutex=CreateMutex(NULL,FALSE,NULL);
WaitForSingleObject(mutex,INFINITE);
_beginthreadex(NULL,NULL,&t,NULL,0,NULL);
WaitForSingleObject(mutex,INFINITE);
printf("HD\n");
}

the result is :

结果是:

HD
:D:D:D

I expect not to see HD in console.....

我希望不要在控制台中看到高清.....

but this code work correctly

但这段代码工作正常

HANDLE mutex;
unsigned _stdcall t(void*){
WaitForSingleObject(mutex,INFINITE);
printf(":D:D:D\n");
ReleaseMutex(mutex);
return NULL;
}
int main(){
mutex=CreateMutex(NULL,FALSE,NULL);
WaitForSingleObject(mutex,INFINITE);
_beginthreadex(NULL,NULL,&t,NULL,0,NULL);
printf("HD\n");
while(1){
}

} 

the result is:

结果是:

HD

Thank you everyone....

谢谢大家....

回答by Georg Fritzsche

As per MSDN:

根据MSDN

The thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its execution.

拥有互斥锁的线程可以在重复等待函数调用中指定相同的互斥锁,而不会阻塞其执行。

Thus in your first sample, the second call to WaitForSingleObject()doesn't block the main thread as it is the thread that owns the mutex.

因此,在您的第一个示例中,第二次调用WaitForSingleObject()不会阻塞主线程,因为它是拥有互斥锁的线程。

回答by Raymond Chen

It looks like you want the main thread to take the mutex on behalf of the secondary thread. Mutexes are tracked by thread, so you cannot take a mutex on behalf of somebody else. You might want to switch to a semaphore, which does not have owner tracking.

看起来您希望主线程代表辅助线程获取互斥锁。互斥锁由线程跟踪,因此您不能代表其他人获取互斥锁。您可能想要切换到没有所有者跟踪的信号量。

回答by Emilio Garavaglia

You used the mutex improperly: when you wait for a mutex you block until somebody else will release it.

您不正确地使用互斥锁:当您等待互斥锁时,您会阻塞直到其他人释放它。

The thread procedure must do its own lock/unlock (WaitForSingleObject and ReleaseMutex, in windows) as well as the main thread. The purpose is avoid that one thread interleave the output with another.

线程过程必须像主线程一样执行自己的锁定/解锁(WaitForSingleObject 和 ReleaseMutex,在 Windows 中)。目的是避免一个线程将输出与另一个线程交错。

If you want to "join" a thread (do something after its termination) you can wait for it as well.

如果你想“加入”一个线程(在它终止后做一些事情)你也可以等待它。

HANDLE mutex;

unsigned _stdcall t(void*)
{
    WaitForSingleObject(mutex,INFINITE);
    printf(":D:D:D\n");
    ReleaseMutex(mutex);
    return NULL;
}

int main()
{
    mutex=CreateMutex(NULL,FALSE,NULL);
    _beginthreadex(NULL,NULL,&t,NULL,0,NULL);
    Sleep(0);
    WaitForSingleObject(mutex,INFINITE);
    printf("HD\n");
    ReleaseMutex(mutex);
} 

Also: don't use infinite loops to stuck a thread (while(1) ...), use Sleepinstead.

另外:不要使用无限循环来卡住线程 ( while(1) ...),Sleep而是使用。

Note also that -in general- you cannot predict what thread will printf first. That's what concurrency is for: the threads compete each other running in parallel. The mutex avoid them to output together, but what sequence will result depends also on what else the system is doing while running your program.

另请注意,一般而言,您无法预测哪个线程将首先打印 printf。这就是并发的用途:线程在并行运行时相互竞争。互斥体避免它们一起输出,但结果的顺序还取决于系统在运行程序时正在做什么。