Reputation: 1015
We have use case where data will be updated at regular intervals. But there will be multiple thread reading the data. So the solution which we are thinking is to use double buffers. So the consumer threads will be reading a foreground page while the producer will update the background page. Once producer updates the background page it will swap the foreground page with this page without taking lock. As the data will be same or different we still don't care as it will not impact the operation. Now the question is how to do the job as I know the traditional producer consumer problem where I can use two buffer for the same job and keep rotating the things but the issue to swap I would need to have a lock but that is what we want to avoid.
So how to perform the things. Any pointer in this regard will be great.
Upvotes: 0
Views: 695
Reputation: 16898
Technically, the actual exchange can be performed by:
std::atomic_echange
, std::atomic_exchange_explicit
lock xchg
__atomic_exchange
or older __sync_lock_test_and_set
InterlockedExchangePointer
Upvotes: 1