no message

master
zcy 2021-10-09 15:07:27 +08:00
parent cd9b8668dc
commit 7f2c81bbb2
2 changed files with 111 additions and 0 deletions

View File

@ -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 <queue>
#include <mutex>
#include <condition_variable>
#include <initializer_list>
template<typename T>
class threadsafe_queue{
private:
// data_queue 访问信号量
mutable std::mutex mut;
mutable std::condition_variable data_cond;
std::queue<T> data_queue;
public:
using value_type= typename std::queue<T>::value_type;
using container_type = typename std::queue<T>::container_type;
threadsafe_queue()=default;
threadsafe_queue(const threadsafe_queue&)=delete;
threadsafe_queue& operator=(const threadsafe_queue&)=delete;
/*
* 使,
* */
template<typename _InputIterator>
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<value_type> list):threadsafe_queue(list.begin(),list.end()){
}
/*
*
* */
void push(const value_type &new_value){
std::lock_guard<std::mutex> lk(mut);
data_queue.push(std::move(new_value));
data_cond.notify_one();
}
/*
* ,
* */
value_type wait_and_pop(){
std::unique_lock<std::mutex> 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<std::mutex> 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<std::mutex> lk(mut);
return data_queue.empty();
}
/*
*
* */
auto size() const->decltype(data_queue.size()){
std::lock_guard<std::mutex> lk(mut);
return data_queue.size();
}
}; /* threadsafe_queue */

View File

@ -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 <mutex>
#include <vector>
#include <stdint.h>
template<typename TYP>
class ThreadSafeVector{
public:
typedef typename std::vector<TYP>::iterator iterator;
typedef typename std::vector<TYP>::const_iterator const_iterator;
Val& operator [](uin32_t index)
{
}
private:
std::vector<TYP> m_data;
std::mutex m_mutex;
}