C++11 C++;:模板的队列包装问题
简短问题:我在C++11 C++;:模板的队列包装问题,c++11,queue,std,mutex,C++11,Queue,Std,Mutex,简短问题:我在SafeQueue::clear()中使用std::is_指针和/或std::is_数组时犯了什么明显的错误(误解?)?目的是检查指针队列,然后检查指针是否恰好是unsigned char*或char*数组 这是在C++11中,包装了std::queue类以带来接近线程安全的东西 #ifndef SAFEQUEUE_H #define SAFEQUEUE_H #include <queue> #include <mutex> #include <ty
SafeQueue::clear()中使用std::is_指针和/或std::is_数组
时犯了什么明显的错误(误解?)?目的是检查指针队列,然后检查指针是否恰好是unsigned char*
或char*
数组
这是在C++11中,包装了std::queue
类以带来接近线程安全的东西
#ifndef SAFEQUEUE_H
#define SAFEQUEUE_H
#include <queue>
#include <mutex>
#include <type_traits>
template <typename T>
class SafeQueue
{
public:
SafeQueue() = default; // default ctor
SafeQueue(const SafeQueue&) = delete; // disable copy
SafeQueue& operator=(const SafeQueue&) = delete; // disable assignment
bool empty() const
{
std::unique_lock<std::mutex> ulock(m_mutex);
return m_queue.empty();
}
T& front() // never called without empty() or size() > 0 check
{
std::unique_lock<std::mutex> lock(m_mutex);
if(!m_queue.empty()) { return m_queue.front(); }
}
void clear()
{
std::unique_lock<std::mutex> lock(m_mutex);
if(m_queue.empty()) { return; } // quick exit
bool isPointers = (std::is_pointer<T>::value) ? true : false; // always returns true on class objects
if(isPointers)
{
//bool isarray = std::is_array<T>::value ? true : false; // always returns true on class objects
bool isarray = (std::is_same<unsigned char*, T>::value || std::is_same<char*, T>::value) ? true : false; // also returns true always
while(!m_queue.empty())
{
if(isarray) { delete[] m_queue.front(); m_queue.front() = nullptr; }
else { delete[] m_queue.front(); m_queue.front() = nullptr; }
m_queue.pop();
}
}
else { std::queue<T>().swap(m_queue); }
}
void pop()
{
std::unique_lock<std::mutex> lock(m_mutex);
if(!m_queue.empty()) { m_queue.pop(); }
}
unsigned int size() const
{
std::unique_lock<std::mutex> lock(m_mutex);
return m_queue.size();
}
void push(const T& item)
{
std::unique_lock<std::mutex> lock(m_mutex);
m_queue.push(item);
}
protected:
mutable std::mutex m_mutex;
std::queue<T> m_queue;
};
#endif // SAFEQUEUE_H
\ifndef SAFEQUEUE\u H
#定义安全队列
#包括
#包括
#包括
样板
类安全队列
{
公众:
SafeQueue()=默认值;//默认值
安全队列(const SafeQueue&)=删除;//禁用复制
SafeQueue&运算符=(const SafeQueue&)=delete;//禁用分配
bool empty()常量
{
std::唯一锁ulock(mumutex);
返回m_queue.empty();
}
T&front()//如果没有空()或size()>0检查,则永远不会调用
{
std::唯一的_锁(m_互斥);
如果(!m_queue.empty()){返回m_queue.front();}
}
无效清除()
{
std::唯一的_锁(m_互斥);
if(m_queue.empty()){return;}//快速退出
bool isPointers=(std::is_pointer::value)?true:false;//始终在类对象上返回true
if(isPointers)
{
//bool isarray=std::is_array::value?true:false;//在类对象上总是返回true
bool isarray=(std::is|u same::value | std::is_same::value)?true:false;//也始终返回true
而(!m_queue.empty())
{
if(isarray){delete[]m_queue.front();m_queue.front()=nullptr;}
else{delete[]m_queue.front();m_queue.front()=nullptr;}
m_queue.pop();
}
}
else{std::queue().swap(m_queue);}
}
void pop()
{
std::唯一的_锁(m_互斥);
如果(!m_queue.empty()){m_queue.pop();}
}
无符号整数大小()常量
{
std::唯一的_锁(m_互斥);
返回m_queue.size();
}
无效推送(常数T和项目)
{
std::唯一的_锁(m_互斥);
m_队列推送(项目);
}
受保护的:
可变std::mutex m_mutex;
std::队列m_队列;
};
#endif//SAFEQUEUE_H
即使控件为false
,也会编译所有分支
您正在生成BuffFract,因为对于<代码> t= int ,Dele[]/Cudio>在<代码> int >不是合法C++()。
解决问题的方法是不存储原始char*
数组,而是在队列中存储智能指针,如std::unique\u ptr
或std::unique\u ptr
您可以使用类型特征执行此操作:
template<class T> struct queued_type{
using type=T;
using extracted_type=T&;
static extracted_type extract(type& out){ return out; }
static type& box(T& in){ return in; }
};
template<class T> struct queued_type<T*>{
using type=std::unique_ptr<T[]>;
using extracted_type=type;
static extracted_type extract(type& out){ return out.release(); }
static type box(type& in){ return type(in); }
};
template<class T>using queued_type_t=typename queued_type<T>::type;
模板结构排队类型{
使用类型=T;
使用提取的_type=T&;
静态提取\u类型提取(type&out){return out;}
静态类型&box(T&in){return in;}
};
模板结构排队类型{
使用type=std::unique\u ptr;
使用提取的类型=类型;
静态提取_类型提取(type&out){return out.release();}
静态类型框(type&in){返回类型(in);}
};
templateusing queued_type_t=typename queued_type::type;
现在存储一个std::queue
push
执行m_队列
,front
执行queued_type::extract(m_queue.front())
并返回typename queued_type::extracted_type
清除的是std::queue{}.swap(m_queue)代码>没有逻辑。您如何知道这些行“总是返回true”?你是怎么想的?手段包括具体问题或错误;你对错误发生原因的解释是不够的。