Run clang-format
This commit is contained in:
@@ -1,56 +1,52 @@
|
||||
#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;
|
||||
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;
|
||||
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(std::move(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>(std::move(data.top())));
|
||||
data.pop();
|
||||
return res;
|
||||
}
|
||||
void pop(T& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if(data.empty()) throw empty_stack();
|
||||
value=std::move(data.top());
|
||||
data.pop();
|
||||
}
|
||||
bool empty() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
return data.empty();
|
||||
}
|
||||
void push(T new_value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
data.push(std::move(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>(std::move(data.top())));
|
||||
data.pop();
|
||||
return res;
|
||||
}
|
||||
void pop(T& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if (data.empty())
|
||||
throw empty_stack();
|
||||
value = std::move(data.top());
|
||||
data.pop();
|
||||
}
|
||||
bool empty() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
return data.empty();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user