尝试锁定 Windows

发布于 2024-11-27 07:04:20 字数 147 浏览 5 评论 0原文

在pthread库中可以找到非阻塞函数:

int pthread_mutex_trylock(pthread_mutex_t *mutex);

我可以在Windows中找到类似的东西吗?

in pthread library it is possible to find non blocking function:

int pthread_mutex_trylock(pthread_mutex_t *mutex);

can I find something similar in Windows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话难免假 2024-12-04 07:04:20

如果您使用锁的关键部分,则等效内容为 TryEnterCriticalSection()

如果您使用互斥锁作为锁,则等效方法是调用 WaitForSingleObject() 传递 0 作为超时。

如果您不熟悉 Windows 同步对象,请不要被愚弄而更喜欢互斥体,因为它有一个您在 pthread 背景中最熟悉的名称。只要您的同步在进程内,关键部分就会更高效且更易于使用。

If you are using a critical section for your lock then the equivalent is TryEnterCriticalSection().

If you are using a mutex for your lock then the equivalent is to call WaitForSingleObject() passing 0 as the timeout.

If you are unfamiliar with Windows synchronisation objects, don't be fooled into preferring the mutex because it has a name that you are most familiar with from a pthreads background. So long as your synchronisation is within process, critical sections are more efficient and easier to use.

谷夏 2024-12-04 07:04:20

您可以检查 Boost 库。他们有一个叫做shareable_locks的东西。< br>
这是 Boost Interprocess Library 的一部分,适用于Windows 和 Unix。

You could check the Boost library. They have something called shareable_locks.
This is part of the Boost Interprocess Library which works in both Windows and Unix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文