r/cpp • u/Beginning-Safe4282 • May 22 '26
Building a Fast Lock-Free Queue in Modern C++ From Scratch
https://jaysmito.dev/blog/blog/04-fast-lockfree-queues/
135
Upvotes
r/cpp • u/Beginning-Safe4282 • May 22 '26
72
u/woppo May 23 '26
You have a member function called IsEmpty:
// This is an extremely handy function which we can add to the // Queue API so that we can use it to check if a queue is filled // or not and this will be very useful for a lot of situations // where you could technically put a thread to sleep based on // this value. bool IsEmpty(uint32_t threadId) { (void)threadId; std::unique_lock lock(m_Mutex); return m_Queue.empty(); }It is not extremley handy. It should be called WasEmpty. It has no use whatsoever.
25
u/Plazmatic May 22 '26
No shade on the article itself, but man, i'll never get used to Unreal Engine code style. Making everything upper case pascal case is such a weird choice, nobody else except people who use UE or are inspired by UE use this style and it makes code more confusing than it needs to be and causes issues that have to be worked around (when a variable is named the same as the object type.... especially annoying in generic code, hence why UE is forced to prefix hungarian notation on every object type because of this god-awful style), and it causes things like OP's code, where it tries to regress to regular PascalCase style/Java style but doesn't commit. Like the reason you separate the styling between functions/variables and types is to avoid name conflicts, why only do that for variables? Like I get people don't want to use stdstyle, or RustCPP style because they don't like snake case or something, but then why not just use regular java style at that point?
4
u/Plazmatic May 23 '26
I'm glad someone made ablog about MPMC queues, way too many about SPSC. I have some critiques but they are mostly code related, not content.
Not copyable because copying a mutex doesn't make sense? Sure. Not movable? That's not justified by merely having a mutex. A common pattern is either to wrap the mutex in a pointer (like a unique_ptr) or wrap multilple shared values in a unique pointer.
Though that doesn't mean it should be movable, it often doesn't even make sense to move a regular queue. Typically queues are shared between multiple locations which must be done by reference/pointer. If you move the queue the reference/pointer to the queue is invalidated. This is even more apparent when threads are involved, so to enforce references being stable, you make the object non-movable, and then force the end user to wrap in a unique ptr to make movable (and also giving single object location that doesn't change on move).
The
<T>here is redundant.Is this an attempt to quell unused variable compiler warnings? I've never seen someone try to do that this way. There are two typical methods to do this, either simply don't name the parameter at all
or us use [[maybe_unused]]
Doing that void thing will cause c style cast warnings to popup.
I'm not sure this is a good mindset and seems very misleading to how the compiler works, or what factors effect performance. The compiler doesn't make a binary decision to inline or not inline, sometimes it inlines a function in one place but not another. Additionally your instruction cache is also a factor in performance, and you forcing inlining here is really bad for objects that are not simple. I feel you really should just let the compiler do it's thing, microbenchmarks can't tell you when this will screw you later, you can't just benchmark these things in isolation to see if forcing inlining is a performance win here or not when put into an actual code base.
Why can't you just use an aligned allocator?
alignas on a std::array is like calling alignas on a T[N] array, it applies to each element already, std::array doesn't store anything beyond the elements themselves, so the only thing to align are those elements.
I would highly recommend you never use c style casts in C++ code, especially in generic template code where it has the highest chance of doing something you didn't expect. The rules for c style casts and pitfalls are not as simple as they appear.
I understand the idea behind using __, but unfortunately starting an identifier with _ is reserved by the standard and risks conflicting with posix and your compiler as well. Basically don't ever create identifiers in C++ that start with underscore.
23
u/trailing_zero_count async enthusiast | TooManyCooks author May 23 '26 edited May 23 '26
A few learnings from my own implementation of a hazard-pointer based MPMC queue.
Performance:
Features:
1
u/nychapo May 23 '26
didnt read the article, but what motivated you to use a ptr based design vs a ring buffer?
1
u/User_Deprecated May 23 '26
it looks like all the success cases use seq_cst, maybe acq_rel would be enough? kind of curious about the choice.
1
u/heyhello0o May 24 '26
Is it just me, or is pushing to the tail and popping from the head the normal behavior for a queue? This implementation does the opposite and it's making my head hurt from constantly needing to convert to the weird syntax
1
u/pepejovi May 29 '26
If OP is the blog owner.. please considering centering the content. Maybe it's just me, but having left-aligned content and leaving half the screen unused is very annoying to read 😃
0
u/[deleted] May 24 '26
[deleted] — view deleted comment
0
u/[deleted] May 22 '26
[deleted] — view deleted comment