C++ 队列在没有推送的情况下弹出
c++ queue pop without push
我在 c++ 运行时应用程序上使用了很多std::queue<uint32_t> Queue
。我需要了解,我可以推送到队列中的最大值数是多少,以及如果我使用任何 push_back() pop_front() 会发生什么。
有时,我的代码在尝试弹出值时会在线程内中断。
PostEvent(uint32_t EventUid)
{
/* Create Event Thread only when
first event received (E_RESTART.COLD) */
if (!isThreadCreated) {
c_thread::start("event");
isThreadCreated = true;
}
Queue.push_back(EventUid);
}
event_thread::运行():
while (isThreadAlive())
{
while (Queue.size())
{
if (!Queue.empty())
{
uint32_t EventUid;
EventUid = Queue[0];
Queue.pop_front();
}
}
}
我还需要知道,我可以使用这样的数据结构吗
struct Counter_Elements
{
uint32_t eventId;
std::string applicationName;
std::string functionName;
std::string dataName;
std::string dataValue;
uint64_t count;
char tag;
};
并创建一个类似 std::queue<Counter_Elements> CountQueue
的队列。如果是这样,我可以推送的最大计数器元素数量是多少。
标准容器仅在只读情况下是线程安全的。例如,您需要在修改队列(推送和/或弹出)时使用互斥锁来保护队列。
通常,您需要从创建线程安全队列开始,如下所示:
template <class T, class Container = std::deque<T>>
class ts_queue {
Container c;
std::mutex m;
public:
void push(T const &v) {
std::lock_guard<std::mutex> l(m);
c.push_back(v);
}
bool pop(T &v) {
std::lock_guard<std::mutex> l(m);
if (c.empty())
return false;
v = c.front();
c.pop_front();
return true;
}
};
这样,在任何给定时间只有一个线程可以修改队列,从而保持队列安全。
请注意,我已经将pop
的签名更改为我发现在进行多线程编程时(更)有用的签名。在典型情况下,您希望为弹出(也可能是推送)添加超时,因此如果在合理的时间内没有可用的数据,您只需放弃并返回 false。就目前而言,这是它的简化版本,如果数据不能立即可用,则返回 false。
相关文章:
- 在没有太多条件句的情况下,我如何避免被零除
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 在未初始化映射的情况下,将值插入到映射的映射中
- 是默认情况下分配给char数组常量的值
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 如何在不产生任何垃圾的情况下获得C中的像素
- 在已经使用Git的情况下减少编译时间
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 如何在没有信号的情况下从C++执行QML插槽
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 为什么在某些情况下不写入此文件?
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 在没有Xcode的情况下在Mac捆绑包中嵌入框架
- 关于在这种情况下消息队列与共享内存的适用性或适用性
- C 控制台应用程序;在牙科诊所情况下,时间优化了队列
- 如何在不遍历优先级队列的情况下获取该队列的最后一个元素
- C++ 队列在没有推送的情况下弹出
- 在fixed_sized<true>的情况下提高无锁队列行为
- 在不使用自旋锁的情况下暂停空队列上的线程
- 如何在没有递归的情况下清除四叉树(也许使用队列?).