A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
LockGuard.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 /*
19  * File: LockGuard.h
20  * Author: Chris
21  *
22  * Created on September 25, 2015, 5:12 PM
23  */
24 
25 #ifndef LOCKGUARD_H
26 #define LOCKGUARD_H
27 
28 
29 #include <pthread.h>
30 #include <iostream>
31 #include <sstream>
32 
33 // simple RAII-style helper to hold a lock for the duration of its own
34 // lifetime, typically determined by some scoped block; similar to
35 // std::lock_guard, but for pthread_mutex_t rather than std::mutex
36 
37 class LockGuard {
38 public:
39  // lock on construction
40  explicit LockGuard(pthread_mutex_t& mutex) : mutex(mutex) {
41  // std :: stringstream ss;
42  // ss << &mutex;
43  // std :: cout << "to get lock at " << ss.str() << std :: endl;
44  pthread_mutex_lock(&mutex);
45  // std :: cout << "got lock at " << ss.str() << std :: endl;
46  }
47 
48  // unlock on destruction
49 
51  // std :: stringstream ss;
52  // ss << &mutex;
53  // std :: cout << "to unlock at " << ss.str() << std :: endl;
54  pthread_mutex_unlock(&mutex);
55  // std :: cout << "unlocked at " << ss.str() << std :: endl;
56  }
57 
58  // forbidden, to avoid double-unlock bugs
59  LockGuard(const LockGuard&) = delete;
60  LockGuard& operator=(const LockGuard&) = delete;
61 
62 private:
63  // underlying system mutex to manage
64  pthread_mutex_t& mutex;
65 };
66 
67 
68 #endif /* LOCKGUARD_H */
pthread_mutex_t & mutex
Definition: LockGuard.h:64
~LockGuard()
Definition: LockGuard.h:50
LockGuard(pthread_mutex_t &mutex)
Definition: LockGuard.h:40
LockGuard & operator=(const LockGuard &)=delete