使用 STL 输出二进制缓冲区

发布于 2024-09-16 07:11:36 字数 155 浏览 2 评论 0原文

我正在尝试使用最能描述为二进制输出队列的东西。简而言之,一个线程将用二进制数据填充队列,另一个线程将从队列中弹出该数据,并将其发送到客户端套接字。

使用 STL 实现此目的的最佳方法是什么?我正在寻找类似 std::queue 的东西,但一次要寻找很多项目。

谢谢

I'm trying to use something that could best be described as a binary output queue. In short, one thread will fill a queue with binary data and another will pop this data from the queue, sending it to a client socket.

What's the best way to do this with STL? I'm looking for something like std::queue but for many items at a time.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油焖大侠 2024-09-23 07:11:36

“二进制数据”是什么意思?只是内存缓冲区?您希望能够一次压入/弹出一个缓冲区吗?然后,您应该将缓冲区包装到一个类中,或者使用 std::vector,并将它们推送/弹出到 std::deque 上。

What does "binary data" mean? Just memory buffers? Do you want to be able push/pop one buffer at a time? Then you should wrap a buffer into a class, or use std::vector<char>, and push/pop them onto std::deque.

随波逐流 2024-09-23 07:11:36

我在多线程环境中的网络通信系统中需要这种东西。

就我而言,我只是用一个处理锁定的对象包装了 std::queue (一般来说,std::queue 不是线程安全的)。队列中的对象只是 char* 样式数组的非常轻量级的包装器。

这些包装器还提供了以下我认为非常有用的成员函数。

insertByte(unsigned int location, char value)
insertWord(unsigned int location, int value)
insertLong(unsigned int location, long value)
getByte/Word/Long(unsigned int location)

这些在这种情况下特别有用,因为单词和长值必须进行字节交换,并且我可以将该问题隔离到最后实际处理它的类。

我们对“大于 4 字节”的二进制数据块做了一些稍微奇怪的事情,当时我认为这会阻止我们使用 std::vector,尽管现在我只是使用它并玩弄它&向量[x]。

I've needed this sort of thing for a network communications system in a multi-threaded environment.

In my case I just wrapped std::queue with an object that handled locking (std::queue is not thread-safe, generally speaking). The objects in the queue were just very lightweight wrappers over char*-style arrays.

Those wrappers also provided the following member functions which I find extremely useful.

insertByte(unsigned int location, char value)
insertWord(unsigned int location, int value)
insertLong(unsigned int location, long value)
getByte/Word/Long(unsigned int location)

These were particularly useful in this context, since the word and long values had to be byteswapped, and I could isolate that issue to the class that actually handled it at the end.

There were some slightly strange things we were doing with "larger than 4 byte" chunks of the binary data, which I thought at the time would prevent us from using std::vector, although these days I would just use it and play around with &vector[x].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文