Run clang-format
This commit is contained in:
@@ -1,57 +1,56 @@
|
||||
#include <climits>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
#include <climits>
|
||||
|
||||
class hierarchical_mutex
|
||||
{
|
||||
std::mutex internal_mutex;
|
||||
unsigned long const hierarchy_value;
|
||||
unsigned long previous_hierarchy_value;
|
||||
static thread_local unsigned long this_thread_hierarchy_value;
|
||||
class hierarchical_mutex {
|
||||
std::mutex internal_mutex;
|
||||
unsigned long const hierarchy_value;
|
||||
unsigned long previous_hierarchy_value;
|
||||
static thread_local unsigned long this_thread_hierarchy_value;
|
||||
|
||||
void check_for_hierarchy_violation()
|
||||
{
|
||||
if(this_thread_hierarchy_value <= hierarchy_value)
|
||||
{
|
||||
throw std::logic_error("mutex hierarchy violated");
|
||||
}
|
||||
}
|
||||
void update_hierarchy_value()
|
||||
{
|
||||
previous_hierarchy_value=this_thread_hierarchy_value;
|
||||
this_thread_hierarchy_value=hierarchy_value;
|
||||
void check_for_hierarchy_violation()
|
||||
{
|
||||
if (this_thread_hierarchy_value <= hierarchy_value) {
|
||||
throw std::logic_error("mutex hierarchy violated");
|
||||
}
|
||||
}
|
||||
void update_hierarchy_value()
|
||||
{
|
||||
previous_hierarchy_value = this_thread_hierarchy_value;
|
||||
this_thread_hierarchy_value = hierarchy_value;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit hierarchical_mutex(unsigned long value):
|
||||
hierarchy_value(value),
|
||||
previous_hierarchy_value(0)
|
||||
{}
|
||||
void lock()
|
||||
{
|
||||
check_for_hierarchy_violation();
|
||||
internal_mutex.lock();
|
||||
update_hierarchy_value();
|
||||
}
|
||||
void unlock()
|
||||
{
|
||||
this_thread_hierarchy_value=previous_hierarchy_value;
|
||||
internal_mutex.unlock();
|
||||
}
|
||||
bool try_lock()
|
||||
{
|
||||
check_for_hierarchy_violation();
|
||||
if(!internal_mutex.try_lock())
|
||||
return false;
|
||||
update_hierarchy_value();
|
||||
return true;
|
||||
}
|
||||
explicit hierarchical_mutex(unsigned long value)
|
||||
: hierarchy_value(value), previous_hierarchy_value(0)
|
||||
{
|
||||
}
|
||||
void lock()
|
||||
{
|
||||
check_for_hierarchy_violation();
|
||||
internal_mutex.lock();
|
||||
update_hierarchy_value();
|
||||
}
|
||||
void unlock()
|
||||
{
|
||||
this_thread_hierarchy_value = previous_hierarchy_value;
|
||||
internal_mutex.unlock();
|
||||
}
|
||||
bool try_lock()
|
||||
{
|
||||
check_for_hierarchy_violation();
|
||||
if (!internal_mutex.try_lock())
|
||||
return false;
|
||||
update_hierarchy_value();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
thread_local unsigned long
|
||||
hierarchical_mutex::this_thread_hierarchy_value(ULONG_MAX);
|
||||
thread_local unsigned long hierarchical_mutex::this_thread_hierarchy_value(
|
||||
ULONG_MAX);
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
hierarchical_mutex m1(42);
|
||||
hierarchical_mutex m2(2000);
|
||||
|
||||
hierarchical_mutex m1(42);
|
||||
hierarchical_mutex m2(2000);
|
||||
}
|
||||
|
||||
110
ch3/stack.cpp
110
ch3/stack.cpp
@@ -1,68 +1,64 @@
|
||||
#include <exception>
|
||||
#include <stack>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <stack>
|
||||
|
||||
struct empty_stack: std::exception
|
||||
{
|
||||
const char* what() const throw()
|
||||
{
|
||||
return "empty stack";
|
||||
}
|
||||
|
||||
struct empty_stack : std::exception {
|
||||
const char* what() const throw() { return "empty stack"; }
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class threadsafe_stack
|
||||
{
|
||||
template <typename T>
|
||||
class threadsafe_stack {
|
||||
private:
|
||||
std::stack<T> data;
|
||||
mutable std::mutex m;
|
||||
public:
|
||||
threadsafe_stack(){}
|
||||
threadsafe_stack(const threadsafe_stack& other)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(other.m);
|
||||
data=other.data;
|
||||
}
|
||||
threadsafe_stack& operator=(const threadsafe_stack&) = delete;
|
||||
std::stack<T> data;
|
||||
mutable std::mutex m;
|
||||
|
||||
void push(T new_value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
data.push(new_value);
|
||||
}
|
||||
std::shared_ptr<T> pop()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if(data.empty()) throw empty_stack();
|
||||
std::shared_ptr<T> const res(std::make_shared<T>(data.top()));
|
||||
data.pop();
|
||||
return res;
|
||||
}
|
||||
void pop(T& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if(data.empty()) throw empty_stack();
|
||||
value=data.top();
|
||||
data.pop();
|
||||
}
|
||||
bool empty() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
return data.empty();
|
||||
}
|
||||
public:
|
||||
threadsafe_stack() {}
|
||||
threadsafe_stack(const threadsafe_stack& other)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(other.m);
|
||||
data = other.data;
|
||||
}
|
||||
threadsafe_stack& operator=(const threadsafe_stack&) = delete;
|
||||
|
||||
void push(T new_value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
data.push(new_value);
|
||||
}
|
||||
std::shared_ptr<T> pop()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if (data.empty())
|
||||
throw empty_stack();
|
||||
std::shared_ptr<T> const res(std::make_shared<T>(data.top()));
|
||||
data.pop();
|
||||
return res;
|
||||
}
|
||||
void pop(T& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if (data.empty())
|
||||
throw empty_stack();
|
||||
value = data.top();
|
||||
data.pop();
|
||||
}
|
||||
bool empty() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
return data.empty();
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
int
|
||||
main()
|
||||
{
|
||||
threadsafe_stack<int> si;
|
||||
si.push(5);
|
||||
si.pop();
|
||||
if(!si.empty())
|
||||
{
|
||||
int x;
|
||||
si.pop(x);
|
||||
}
|
||||
|
||||
threadsafe_stack<int> si;
|
||||
si.push(5);
|
||||
si.pop();
|
||||
if (!si.empty()) {
|
||||
int x;
|
||||
si.pop(x);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user