r/cpp • • Mar 24 '26

Optimizing a Lock-Free Ring Buffer

https://david.alvarezrosa.com/posts/optimizing-a-lock-free-ring-buffer/
104 Upvotes

58 comments sorted by

View all comments

Show parent comment

7

u/ack_error Mar 24 '26

This is not a good way to allocate adjacent memory views in current versions of Windows due to the race between the VirtualFree() and the map calls. While it has a retry loop, there's no guarantee of forward progress, particularly if there is a second instance of this same loop on another thread.

The correct way to do this is to use VirtualAlloc2() with MEM_RESERVE_PLACEHOLDER and then MapViewOfFile3() with MEM_REPLACE_PLACEHOLDER.

2

u/rzhxd Mar 24 '26

Thanks, I'll look into these functions. Mainly doing development and debugging on Linux, so just slapped whatever was first in there.