Ccriticalsection 可以在生产中使用吗?

2022-01-12 00:00:00 multithreading visual-c++ mfc

我们是 MFC 的几个新手,我们正在构建一个多线程应用程序.我们在 URL 中遇到了警告我们不要使用 CCriticalSection 的文章,因为它的实现被破坏了.我们很想知道是否有人有使用 CCriticalSection 的经验,您是否遇到任何问题或错误?如果我们使用 VC++ 2008 构建我们的应用程序,CCriticalSection 是否可用并准备好生产?

We are a couple of newbies in MFC and we are building a multi-threded application. We come across the article in the URL that warns us not to use CCriticalSection since its implementation is broken. We are interested to know if anyone has any experience in using CCriticalSection and do you come across any problems or bugs? Is CCriticalSection usable and production ready if we use VC++ 2008 to build our application?

http://www.flounder.com/avoid_mfc_syncrhonization.htm

谢谢

推荐答案

我认为这篇文章是基于对 CSingleLock 的用途和使用方法的根本误解.

I think that article is based on a fundamental misunderstanding of what CSingleLock is for and how to use it.

您不能多次锁定同一个 CSingleLock,但您不应该这样做.CSingleLock,顾名思义,就是用来锁定某个东西一次.

You cannot lock the same CSingleLock multiple times, but you are not supposed to. CSingleLock, as its name suggests, is for locking something ONCE.

每个 CSingleLock 只管理其他对象上的一个锁(例如,您在构造期间传递它的 CCriticalSection),目的是在 CSingleLock 超出范围时自动释放该锁.

Each CSingleLock just manages one lock on some other object (e.g. a CCriticalSection which you pass it during construction), with the aim of automatically releasing that lock when the CSingleLock goes out of scope.

如果你想多次锁定底层对象,你会使用多个 CSingleLocks;您不会使用单个 CSingleLock 并尝试多次锁定它.

If you want to lock the underlying object multiple times you would use multiple CSingleLocks; you would not use a single CSingleLock and try to lock it multiple times.

错误(他的例子):

CCriticalSection crit;
CSingleLock lock(&crit);
lock.Lock();
lock.Lock();
lock.Unlock();
lock.Unlock();

对:

CCriticalSection crit;
CSingleLock lock1(&crit);
CSingleLock lock2(&crit);
lock1.Lock();
lock2.Lock();
lock2.Unlock();
lock1.Unlock();

更好(这样你就可以得到 RAII):

Even better (so you get RAII):

CCriticalSection crit;
// Scope the objects
{
    CSingleLock lock1(&crit, TRUE); // TRUE means it (tries to) locks immediately.
    // Do stuff which needs the lock (if IsLocked returns success)
    CSingleLock lock2(&crit, TRUE);
    // Do stuff which needs the lock (if IsLocked returns success)
}
// crit is unlocked now.

(当然,你永远不会在这样的单个块中故意在同一个底层关键部分上获得两个锁.这通常只会发生在调用函数的结果中,这些函数在已经拥有的其他东西中获得了锁自己的锁.)

(Of course, you would never intentionally get two locks on the same underlying critical section in a single block like that. That'd usually only happen as a result of calling functions which get a lock while inside something else that already has its own lock.)

(另外,您应该检查 CSingleLock.IsLocked 以查看锁定是否成功.为了简洁起见,我已将这些检查排除在外,因为它们被排除在原始示例之外.)

(Also, you should check CSingleLock.IsLocked to see if the lock was successful. I've left those checks out for brevity, and because they were left out of the original example.)

如果 CCriticalSection 本身也遇到同样的问题,那肯定是个问题,但他没有提供我能看到的证据.(也许我错过了一些东西.我在 MFC 安装中也找不到 CCriticalSection 的源来验证这种方式.)

If CCriticalSection itself suffers from the same problem then that certainly is a problem, but he's presented no evidence of that that I can see. (Maybe I missed something. I can't find the source to CCriticalSection in my MFC install to verify that way, either.)

相关文章