need a good solution for synchronized read write

妖精的绣舞 提交于 2019-12-11 16:25:00

问题


I have a thread(WRITER) that continuously populates a buffer(a vector in my case) as the incoming data arrive. And i have another thread(READER) that periodically checks the buffer for incoming data and clears the buffer after processing data is done. Once I saw an implementation that suggested to have two buffers (A and B) instead. the WRITER writes into buffer A, and when it is the time for reading the data, the READER takes over buffer A and WRITER starts reading into B...and this flipping continues.

I googled for a reference or sample code but couldn't find any. Can you please suggest me a reference? Also, is this the best and simplest approach?

thank you


回答1:


The simplest solution is using a syncronized queue with the help of the Boost libraries.

There is an article about this on CodeProject here, and countless examples on Google.




回答2:


You can find many samples and tips for implementing a synchronized queue:

Implementing a Thread-Safe Queue using Condition Variables

C++11 Concurrency Tutorial – Part 3: Advanced locking and condition variables



来源:https://stackoverflow.com/questions/15702064/need-a-good-solution-for-synchronized-read-write

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