如何在VC中创建锁?
假设我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要关键部分:
InitializeCriticalSection
< /a> 从任何线程(但通常是主线程)调用一次来初始化锁。在用它做任何其他事情之前先初始化。EnterCriticalSection
调用从任何线程获取锁。如果另一个线程拥有锁,它将阻塞,直到它可以获得锁。临界区是可重入的,这意味着线程可以成功获取锁,即使它已经持有锁。离开CriticalSection
释放锁。对EnterCriticalSection
的每次调用都必须与对LeaveCriticalSection
的匹配调用配对。不要让异常阻止这些获取/释放调用的配对。DeleteCriticalSection
从任何线程(但通常是主线程)调用一次以完成锁定。当没有线程持有锁时执行此操作。调用此方法后,锁无效,您无法尝试再次获取它。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 toEnterCriticalSection
must be paired with a matching call toLeaveCriticalSection
. 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.
通过 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.
如果您使用的是 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).