这段代码是一个有效的关键部分包装类吗

发布于 2024-12-08 13:16:48 字数 1293 浏览 1 评论 0原文

我实际上有两个问题,这是第一个问题。

使用我在两个不同站点找到的代码,我编写了这两个关键部分包装类。

它会起作用吗?

#ifndef CRITICALSECTION_H
#define CRITICALSECTION_H
#include "windows.h"



class CriticalSection{
    long m_nLockCount;
    long m_nThreadId;
    typedef CRITICAL_SECTION cs;
    cs m_tCS;
public:
    CriticalSection(){
        ::InitializeCriticalSection(&m_tCS);
        m_nLockCount = 0;
        m_nThreadId = 0;
    }
    ~CriticalSection(){ ::DeleteCriticalSection(&m_tCS); }
    void Enter(){ ::EnterCriticalSection(&m_tCS);  }
    void Leave(){  ::LeaveCriticalSection(&m_tCS); }
    void Try();
};


class LockSection{
    CriticalSection* m_pCS;
public:
    LockSection(CriticalSection* pCS){
        m_pCS = pCS;
        if(m_pCS)m_pCS->Enter();
    }
    ~LockSection(){
        if(m_pCS)m_pCS->Leave();
    }
}

/*

Safe class basic structure;

class SafeObj
{
     CriticalSection m_cs;

public:
    void SafeMethod()
    {
        LockSection myLock(&m_cs);
        //add code to implement the method ...

    }
};



*/
#endif

第二个问题。在浏览此处时,我注意到作者做了不包括

::Initialize、Delete、Enter、Leave关键部分。这些不是班级正常运作所需要的吗?或者我错过了什么?

I've actually got 2 questions, heres the first one.

Using the code I found at two different sites, I wrote those two critical section wrapper classes.

Is it going to work?

#ifndef CRITICALSECTION_H
#define CRITICALSECTION_H
#include "windows.h"



class CriticalSection{
    long m_nLockCount;
    long m_nThreadId;
    typedef CRITICAL_SECTION cs;
    cs m_tCS;
public:
    CriticalSection(){
        ::InitializeCriticalSection(&m_tCS);
        m_nLockCount = 0;
        m_nThreadId = 0;
    }
    ~CriticalSection(){ ::DeleteCriticalSection(&m_tCS); }
    void Enter(){ ::EnterCriticalSection(&m_tCS);  }
    void Leave(){  ::LeaveCriticalSection(&m_tCS); }
    void Try();
};


class LockSection{
    CriticalSection* m_pCS;
public:
    LockSection(CriticalSection* pCS){
        m_pCS = pCS;
        if(m_pCS)m_pCS->Enter();
    }
    ~LockSection(){
        if(m_pCS)m_pCS->Leave();
    }
}

/*

Safe class basic structure;

class SafeObj
{
     CriticalSection m_cs;

public:
    void SafeMethod()
    {
        LockSection myLock(&m_cs);
        //add code to implement the method ...

    }
};



*/
#endif

And the second question. While browsing here, I noticed that the author did not include

::Initialize , Delete , Enter , Leave critical sections . Aren't these needed for the class to work properly? Or am I missing something?

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

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

发布评论

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

评论(2

就是爱搞怪 2024-12-15 13:16:48

LockSection 类使用 RAII 调用 进入离开。创建对象时,将调用 Enter。当对象被销毁(通过超出范围)时,将调用 Leave

InitializeDeleteCriticalSection 类的构造函数和析构函数调用。

学RAII,学好。这是你的朋友。

The LockSection class uses RAII to call Enter and Leave. When the object is created, the Enter is called. When the object is destroyed (by going out of scope) the Leave is called.

Initialize and Delete are called by the CriticalSection class' constructor and destructor.

Learn RAII, learn it well. It is your friend.

一身软味 2024-12-15 13:16:48

我来回答第一个问题。第二个问题涉及不同站点的代码,您一次只能问一个问题。也许其他人会回答第二个问题。

您包含的代码将正常工作。成员m_nLockCountm_nThreadId 未使用且不需要。 Try() 方法没有实现,但您可以将其删除。

就个人而言,如果未分配 m_pCS,我会引发异常。这是一个明显的错误情况。默默地继续并假装资源受到保护是明显的危险。

I'll answer the first question. The second question involves code at a different site and you should only ask one question at a time. Perhaps someone else will answer the second question.

The code you included will work correctly. The members m_nLockCount and m_nThreadId are unused and not needed. The Try() method has no implementation but you can just delete it

Personally I would raise an exception if m_pCS is not assigned. That's a clear error condition. Silently continuing and pretending that the resource is protected is an obvious danger.

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