使用 CriticalSection 创建类似互斥锁的程序

发布于 2024-10-31 16:10:03 字数 153 浏览 1 评论 0原文

例如,

EnterCriticalSection ( cs );
LeaveCriticalSection ( cs );

我想创建一个锁定它的函数,并在调用您的函数调用或离开该对象时释放它。

如何开始上课?

e.g.

EnterCriticalSection ( cs );
LeaveCriticalSection ( cs );

I want to create a function locking it and release if invoke your function call or leave the object.

How can get started to work out the class?

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

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

发布评论

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

评论(2

找回味觉 2024-11-07 16:10:03

那么有作用域的 CriticalSection 吗?

class ScopedCriticalSection {
   CRITICAL_SECTION cs;

   ScopedCriticalSection()
   {
      if (!InitializeCriticalSectionAndSpinCount(&cs, 0x00000400))
         throw std::runtime_error("Could not initialise CriticalSection object");
      EnterCriticalSection(&cs);
   }

   ~ScopedCriticalSection()
   {
      LeaveCriticalSection(&cs);
      DeleteCriticalSection(&cs);
   }
};

void foo()
{
   ScopedCriticalSection scs;

   /* code! */
}

或者考虑使用Boost互斥体

So a scoped CriticalSection?

class ScopedCriticalSection {
   CRITICAL_SECTION cs;

   ScopedCriticalSection()
   {
      if (!InitializeCriticalSectionAndSpinCount(&cs, 0x00000400))
         throw std::runtime_error("Could not initialise CriticalSection object");
      EnterCriticalSection(&cs);
   }

   ~ScopedCriticalSection()
   {
      LeaveCriticalSection(&cs);
      DeleteCriticalSection(&cs);
   }
};

void foo()
{
   ScopedCriticalSection scs;

   /* code! */
}

Or consider a Boost mutex.

时光暖心i 2024-11-07 16:10:03

您可以将关键部分包装在具有公共函数 acquirereleaseMutex 类中,并使用第二个名为 ScopedLock 的类> 在构造时获取互斥体并在销毁时释放它。

互斥锁:

class Mutex {
   public:
     Mutex() {
       //TODO: create cs
     }
     ~Mutex() {
       //TODO: destroy cs
     }
     void acquire() {
       EnterCriticalSection(cs);
     }
     void release() {
       LeaveCriticalSection(cs);
     }
   private:
     LPCRITICAL_SECTION cs;
     Mutex(const Mutex&); //non-copyable
     Mutex& operator=(const Mutex&); //non-assignable
};

锁:

class ScopedLock {
  public:
    ScopedLock(Mutex* mutex_) : mutex(mutex_) {
      mutex->acquire();
    }
    ~ScopedLock() {
      mutex->release();
    }
  private:
    Mutex* mutex;
};

像这样使用它:

Mutex someMutex;

void foo() {
  ScopedLock lock(&someMutex);
  //critical stuff here
}

void bar() {
  ScopedLock lock(&someMutex);
  //other critical stuff here
}

You could wrap the critical section in a Mutex class with public functions acquire and release and have a second class called ScopedLock acquire the mutex on construction and release it on destruction.

The Mutex:

class Mutex {
   public:
     Mutex() {
       //TODO: create cs
     }
     ~Mutex() {
       //TODO: destroy cs
     }
     void acquire() {
       EnterCriticalSection(cs);
     }
     void release() {
       LeaveCriticalSection(cs);
     }
   private:
     LPCRITICAL_SECTION cs;
     Mutex(const Mutex&); //non-copyable
     Mutex& operator=(const Mutex&); //non-assignable
};

The Lock:

class ScopedLock {
  public:
    ScopedLock(Mutex* mutex_) : mutex(mutex_) {
      mutex->acquire();
    }
    ~ScopedLock() {
      mutex->release();
    }
  private:
    Mutex* mutex;
};

Use it like this:

Mutex someMutex;

void foo() {
  ScopedLock lock(&someMutex);
  //critical stuff here
}

void bar() {
  ScopedLock lock(&someMutex);
  //other critical stuff here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文