string producer/consumer in C++: std::deque<char> or std::stringstream?

余生颓废 提交于 2019-12-11 01:06:19

问题


In our application we have a class that produces characters, and another that consumes them. The current implementation dynamically allocates characters as they are produced (using new) and delete them (with delete) as they are consumed. This is all terribly slow, and I am looking at ways to replace that implementation to improve its performance.

The semantic I need is that of the standard class queue: push at the front, pop at the back. The default implementation uses a deque IIRC. deque is typically implemented using "blocks" or "chunks" of memory, so I expect far less calls to the OS memory allocator, and a significant speed-up, with little additional memory usage.

However, since the data queued is characters (possibly wide characters), an alternative would be to use the standard input/output stream class, namely the character stream stringstream. AFAIK, their behaviour is queue-like too.

Is there a better choice a priori? Would both classes have similar allocation patterns? I can try and measure performance of both, but perhaps it doesn't really matter and either would be good enough. In that case, which would be easiest/safest to use?

A secondary matter is concurrency between the producer and the consumer. I can restrict access to be sequential (on the same thread), but a thread-safe implementation is likely to be beneficial performance-wise with current multi-core hardware.

Thanks for your wisdom before I dive in and start coding.


回答1:


std::stringstream isn't a queue, since reading characters doesn't consume them. You can simply seekg(0) and read the same characeters all over again. Thus, the more you write, the more memory you will consume.

Stick with std::queue. The default choice of std::deque as the underlying implementation is almost certainly the right one.

Regarding concurrency, it is categorically unsafe to write to std::queue while any other thread is reading or writing to it. If you want an efficient blocking queue implementation, you'll have to write or borrow one specifically built for the purpose.




回答2:


I read this many years ago (I think, did not read it again) when they actually used to send me the magazine before they started asking for silly money for it before they went bust :-).

This might help

http://www.drdobbs.com/parallel/lock-free-queues/208801974

As a side note I work on real time systems processing financial data - and we use fixed length queues generally and throw away data that will not fit in the queue if the consumer cannot processes it - old data is worse than missed data. Of course your requirement may differ.



来源:https://stackoverflow.com/questions/12700563/string-producer-consumer-in-c-stddequechar-or-stdstringstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!