如何改变的Windows互斥的递归锁定行为? [英] How to alter the recursive locking behaviour of Windows Mutex?

查看:328
本文介绍了如何改变的Windows互斥的递归锁定行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

视窗互斥似乎允许如果线程当前持有锁尝试获取它再次获得被收购锁(递归)。

Windows Mutex seems to allow an acquired lock to be acquired again (recursively) if the thread currently owning the lock tries to acquire it.

不过,基于POSIX pthread的锁不允许这样的行为。

But, posix based pthread locks don't allow such a behaviour.

有没有编译时宏或任何设置,可以使窗户互斥的行为以同样的方式作为Pthread互斥?

Is there any compile time macro or any settings which can make the windows mutex behave in the same way as the pthread mutex?

推荐答案

只要你在Windows程序,避免重新实现其互斥的行为。它被重新入由同一个线程是其定义的行为绝对必要的。

As long as you program in Windows, avoid reimplementing the behavior of its Mutex. It being re-entrant by the same thread is absolutely essential for its defined behavior.

无螺纹亲和力的同步对象是计数到1.使用CreateSemaphore()。

A sync object without thread affinity is a semaphore that counts to 1. Use CreateSemaphore().

FWIW,这是非常奇怪的,你需要这样的行为。这听起来像您试图不适当地使用在多个地方相同的同步对象。您可以使用信号,但你会失去并发的潜力。请考虑使用多个互斥锁。

Fwiw, it is very curious that you need this kind of behavior. It sounds like you are trying to use the same sync object in multiple places inappropriately. You can use the semaphore but you'll lose concurrency potential. Consider using more than one mutex instead.

这篇关于如何改变的Windows互斥的递归锁定行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆