diff --git a/general/src/threadsafe/threadsafe_queue.hpp b/general/src/threadsafe/threadsafe_queue.hpp new file mode 100644 index 0000000..ca8914c --- /dev/null +++ b/general/src/threadsafe/threadsafe_queue.hpp @@ -0,0 +1,85 @@ +/* + * @Author: your name + * @Date: 2021-10-09 13:50:19 + * @LastEditTime: 2021-10-09 13:54:44 + * @LastEditors: Please set LastEditors + * @Description: In User Settings Edit + * @FilePath: \test_algorithm\threadsafe_queue.hpp + */ +#include +#include +#include +#include + +template +class threadsafe_queue{ +private: + // data_queue 访问信号量 + mutable std::mutex mut; + mutable std::condition_variable data_cond; + std::queue data_queue; +public: + using value_type= typename std::queue::value_type; + using container_type = typename std::queue::container_type; + threadsafe_queue()=default; + threadsafe_queue(const threadsafe_queue&)=delete; + threadsafe_queue& operator=(const threadsafe_queue&)=delete; + /* + * 使用迭代器为参数的构造函数,适用所有容器对象 + * */ + template + threadsafe_queue(_InputIterator first, _InputIterator last){ + for(auto itor=first;itor!=last;++itor){ + data_queue.push(*itor); + } + } + explicit threadsafe_queue(const container_type &c):data_queue(c){} + /* + * 使用初始化列表为参数的构造函数 + * */ + threadsafe_queue(std::initializer_list list):threadsafe_queue(list.begin(),list.end()){ + } + /* + * 将元素加入队列 + * */ + void push(const value_type &new_value){ + std::lock_guard lk(mut); + data_queue.push(std::move(new_value)); + data_cond.notify_one(); + } + /* + * 从队列中弹出一个元素,如果队列为空就阻塞 + * */ + value_type wait_and_pop(){ + std::unique_lock lk(mut); + data_cond.wait(lk,[this]{return !this->data_queue.empty();}); + auto value = std::move(data_queue.front()); + data_queue.pop(); + return value; + } + /* + * 从队列中弹出一个元素,如果队列为空返回false + * */ + bool try_pop(value_type& value){ + std::lock_guard lk(mut); + if(data_queue.empty()) + return false; + value = std::move(data_queue.front()); + data_queue.pop(); + return true; + } + /* + * 返回队列是否为空 + * */ + auto empty() const->decltype(data_queue.empty()) { + std::lock_guard lk(mut); + return data_queue.empty(); + } + /* + * 返回队列中元素数个 + * */ + auto size() const->decltype(data_queue.size()){ + std::lock_guard lk(mut); + return data_queue.size(); + } +}; /* threadsafe_queue */ diff --git a/general/src/threadsafe/threadsafe_vector.hpp b/general/src/threadsafe/threadsafe_vector.hpp new file mode 100644 index 0000000..3cc9a7d --- /dev/null +++ b/general/src/threadsafe/threadsafe_vector.hpp @@ -0,0 +1,26 @@ +/* + * @Author: your name + * @Date: 2021-10-09 11:24:40 + * @LastEditTime: 2021-10-09 13:45:00 + * @LastEditors: Please set LastEditors + * @Description: In User Settings Edit + * @FilePath: \test_algorithm\threadsafe_vector.hpp + */ +#include +#include +#include + +template +class ThreadSafeVector{ +public: + typedef typename std::vector::iterator iterator; + typedef typename std::vector::const_iterator const_iterator; + Val& operator [](uin32_t index) + { + + } + +private: + std::vector m_data; + std::mutex m_mutex; +} \ No newline at end of file