如何在VC中创建锁?

发布于 2024-10-18 08:30:41 字数 54 浏览 2 评论 0原文

假设我正在 VC++ 中实现一个临界区并保护一些数组,我该如何在 VC++ 中使用锁来实现呢?

Lets say I am implementing a critical section and protecting some array in VC++, how do I do it using locks in VC++?

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

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

发布评论

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

评论(3

断桥再见 2024-10-25 08:30:41

您需要关键部分:

MSDN 提供了一个简单示例

如果您使用的是 MFC,那么您可能会使用 < code>CCriticalSection 将 Win32 临界区 API 包装在一个类中。

至于你如何用你的阵列做到这一点。好吧,您的线程一次只会执行受锁保护的代码块。您需要锁来阻止两个线程尝试同时读/写同一内存位置的竞争条件,或者其他可能破坏算法的更微妙的条件。

如果您要描述该数组、其内容以及如何对其进行操作,那么可能会给您一些具体的建议。您在此阵列上的具体操作方式将对理想的同步策略产生很大影响,并且在某些情况下您可能能够使用无锁方法。

You need the API functions for critical sections:

  • InitializeCriticalSection Call once, from any thread, but typically the main thread, to initialize the lock. Initialize before you do anything else with it.
  • EnterCriticalSection Call from any thread to acquire the lock. If another thread has the lock, it will block until it can acquire the lock. Critical sections are re-entrant meaning a thread successfully acquires the lock even if it already holds it.
  • LeaveCriticalSection Release the lock. Each call to EnterCriticalSection must be paired with a matching call to LeaveCriticalSection. Don't let exceptions stop these acquire/release calls being paired up.
  • DeleteCriticalSection Call once, from any thread, but typically the main thread, to finalize the lock. Do this when no threads hold the lock. After you call this the lock is invalid and you can't attempt to acquire it again.

MSDN helpfully provide a trivial example.

If you are using MFC then you would probably use CCriticalSection which wraps up the Win32 critical section APIs in a class.

As for how you do it with your array. Well, your threads will only execute blocks of code protected by the lock one at a time. You need the lock to stop race conditions where two threads try to read/write to the same memory location simultaneously, or indeed other more subtle conditions that can break your algorithm.

If you were to describe the array, its contents, and how you operate on it, then it might be possible to give you some specific advice. Exactly how you operate on this array will have a large bearing on the ideal synchronisation strategy, and in certain cases you may be able to use lock-free methods.

听不够的曲调 2024-10-25 08:30:41

通过 CreateMutex 创建互斥体,并取得所有权通过 WaitForSingleObject,释放互斥体的所有权通过 ReleaseMutex,并在您需要时将其删除使用 CloseHandle 完成。

您可以查找的替代方案包括 CriticalSections、< a href="http://msdn.microsoft.com/en-us/library/ms682438%28VS.85%29.aspx" rel="nofollow">信号量 和 事件

Create a mutex via CreateMutex, take ownership of it via WaitForSingleObject, release ownership of the mutex via ReleaseMutex, and deleted it when you are done with CloseHandle.

Alternatives you can look up include CriticalSections, Semaphores, and Events.

神魇的王 2024-10-25 08:30:41

如果您使用的是 VS 2010,则 criticial_section对象包含在头文件ppl.h中。

请注意,还有一个同步的concurrent_vector 类模板(即不需要锁)。

If you're using VS 2010, a criticial_section object is included in the header file ppl.h.

Note there is also a concurrent_vector class template which is synchronized (i.e. locks aren't needed).

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