作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

39 rader
1001 B

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #ifndef STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_
  5. #define STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_
  6. #include "port/port.h"
  7. namespace leveldb {
  8. // Helper class that locks a mutex on construction and unlocks the mutex when
  9. // the destructor of the MutexLock object is invoked.
  10. //
  11. // Typical usage:
  12. //
  13. // void MyClass::MyMethod() {
  14. // MutexLock l(&mu_); // mu_ is an instance variable
  15. // ... some complex code, possibly with multiple return paths ...
  16. // }
  17. class MutexLock {
  18. public:
  19. explicit MutexLock(port::Mutex *mu) : mu_(mu) {
  20. this->mu_->Lock();
  21. }
  22. ~MutexLock() { this->mu_->Unlock(); }
  23. private:
  24. port::Mutex *const mu_;
  25. // No copying allowed
  26. MutexLock(const MutexLock&);
  27. void operator=(const MutexLock&);
  28. };
  29. }
  30. #endif // STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_