RAII 封装锁和CountDownLatch实现

 1 class noncopyable
 2 {
 3 public:
 4     noncopyable(){}
 5     ~noncopyable(){}
 6 private:
 7     noncopyable(const noncopyable &){}
 8     noncopyable &operator=(const noncopyable&){}
 9 };
10 
11 class MyMutex : public noncopyable
12 {
13 public:
14     MyMutex(){pthread_mutex_init(&_mutex,NULL);}
15     ~MyMutex(){pthread_mutex_destroy(&_mutex);}
16 public:
17     void Lock(){pthread_mutex_lock(&_mutex);}
18     void Unlock(){pthread_mutex_unlock(&_mutex);}
19     inline pthread_mutex_t &get(){return _mutex;}
20 private:
21     pthread_mutex_t _mutex;
22 };
23 
24 class MyMutexGuard : public noncopyable
25 {
26 public:
27     explicit MyMutexGuard(MyMutex &mutex) : _mutex(mutex){_mutex.Lock();}
28     ~MyMutexGuard(){_mutex.Unlock();}
29 private:
30     MyMutex& _mutex;
31 };
32 
33 class MyContidition : public noncopyable
34 {
35 public:
36     explicit MyContidition(MyMutex &mutex) : _mutex(mutex.get()){
37         pthread_cond_init(&_cond,NULL);
38     }
39     ~MyContidition(){}
40 public:
41     void notify(){pthread_cond_signal(&_cond);}
42     void notifyall(){pthread_cond_broadcast(&_cond);}
43     void wait(){pthread_cond_wait(&_cond,&_mutex);}
44 private:
45     pthread_cond_t _cond;
46     pthread_mutex_t& _mutex;
47 };
48 
49 
50 class CountDownLatch : public noncopyable
51 {
52 public:
53     explicit CountDownLatch(int count) : _count(count),_cond(_mutex){}
54     ~CountDownLatch(){}
55 private:
56     int _count;
57     MyMutex _mutex;
58     MyContidition _cond;
59 public:
60     void CountDown(){
61         MyMutexGuard __access__(_mutex);
62         --_count;
63         if(_count == 0){
64             _cond.notifyall();
65         }
66     }
67 
68     void Wait(){
69         MyMutexGuard __access__(_mutex);
70         if(_count > 0){
71             _cond.wait();
72         }
73     }
74 };

 

转载于:https://www.cnblogs.com/gtxvs/p/9020321.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RAII(Resource Acquisition Is Initialization)是一种C++编程技巧,它的主要思想是:在对象的构造函数中获取资源,在析构函数中释放资源。这样可以确保资源的正确释放,即使在发生异常时也不会出错。 实现RAII的一般步骤如下: 1. 创建一个类来管理资源,并在构造函数中获得该资源。例如,如果你需要管理一个文件句柄,你可以创建一个文件句柄类,它的构造函数打开文件并返回一个文件句柄。 2. 在类的析构函数中释放资源。例如,在文件句柄类的析构函数中,你应该关闭文件句柄。 3. 使用这个类创建一个对象,并将其保存在一个自动变量中。这样,在函数的末尾,当自动变量离开作用域时,资源将自动释放。 下面是一个简单的示例,展示如何使用RAII来管理文件句柄: ```c++ #include <iostream> #include <fstream> class File { public: File(const char* filename) : m_file(filename) { std::cout << "File " << filename << " opened.\n"; } ~File() { if (m_file.is_open()) { m_file.close(); std::cout << "File closed.\n"; } } // Example method to write to the file void write(const char* data) { m_file << data; } private: std::ofstream m_file; }; int main() { File file("example.txt"); file.write("Hello, world!"); return 0; } ``` 在上述示例中,我们创建了一个名为File的类,它负责管理文件句柄。在构造函数中打开文件,并在析构函数中关闭文件。在main函数中,我们使用File类来打开文件并写入数据。当程序退出main函数时,File对象离开作用域,析构函数被自动调用,文件句柄被关闭。 这就是RAII的基本思想。通过使用RAII,我们可以确保资源的正确释放,避免了因为异常而导致资源泄漏的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值