/*! * \file threadsafe_list.hpp * \date 2018/11/30 * * \author pan yingdong * Contact: bob.pan@hzleaper.com * * * \note */ #ifndef _THREADSAFE_LIST_H_ #define _THREADSAFE_LIST_H_ #include #include #include #include #include namespace SystemLog { struct empty_list :std::exception { const char* what() const throw() { return "empty list"; }; }; template class threadsafe_list { private: std::list dataList; mutable std::mutex m; public: threadsafe_list() {} threadsafe_list(const threadsafe_list& other) { std::lock_guard guard(m); dataList = other.dataList; } threadsafe_list& operator=(const threadsafe_list&) = delete; bool push_back(T new_val) { std::lock_guard guard(m); dataList.push_back(new_val); return true; } std::shared_ptr pop_front() { std::lock_guard guard(m); if (dataList.empty()) throw empty_list(); std::shared_ptr const res(std::make_shared(dataList.front())); dataList.pop_front(); return res; } bool pop_front(T& value) { std::lock_guard guard(m); if (dataList.empty()) { //throw empty_list(); return false; } value = dataList.front(); dataList.pop_front(); return true; } bool empty() const { std::lock_guard guard(m); return dataList.empty(); } int getSize() const { std::lock_guard guard(m); return dataList.size(); } void clear() { std::lock_guard guard(m); dataList.clear(); } }; } #endif