临界区可用于生产吗? [英] is Ccriticalsection usable in production?

查看:96
本文介绍了临界区可用于生产吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是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

thx

推荐答案

我认为这篇文章是基于对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.)

这篇关于临界区可用于生产吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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