A Resizable Concurrent Map

Jeff Preshing · Feb 22, 2016

AI summary AI-generated, may contain mistakes

The article describes the Linear map from the Junction project, a concurrent hash map that improves on the earlier 'Crude' lock-free hash table by supporting both deletion and resizing. It explains how resizing works by atomically replacing a root pointer with a new, larger table and safely migrating entries using Redirect markers to prevent concurrent updates from being lost, along with a jobCoordinator that lets multiple threads participate in migrations. The author notes that Junction's Leapfrog and Grampa maps build on the same design, and shares the implementation as an educational example of concurrent programming.

In an earlier post, I showed how to implement the “world’s simplest lock-free hash table” in C++. It was so simple that you couldn’t even delete entries or resize the table. Well, a few years have passed since then, and I’ve recently written some concurrent maps without those limitations. You’ll find them in my Junction project on GitHub.

Junction contains several concurrent maps – even the ‘world’s simplest’ is there, under the name ConcurrentMap_Crude. For brevity, let’s call that one the Crude map. In this post, I’ll explain the difference between the Crude map and Junction’s Linear map. Linear is the simplest Junction map that supports both resize and delete.

You can review the original post for an explanation of how the Crude map works. To recap: It’s based on open addressing and linear probing. That means it’s basically a big array of keys and values using a linear search. When inserting or looking up a given key, you hash the key to determine where to begin the search. Concurrent inserts and lookups are permitted.

Keep reading

Out of respect for the author only the opening is shown here. Read the rest on the original site, or in HorizonReader with highlights, notes and AI help.

More from Preshing on Programming

How C++ Resolves a Function Call

AI summaryThe article explains the hidden compile-time algorithm that every C++ compiler uses to resolve a function call, which the author was motivated to understand after repeatedly encountering confusing errors about 'overloads with similar conversions.' It walks through the three main phases of the process—name lookup (including argument-dependent lookup), template argument deduction and substitution (introducing SFINAE), and overload resolution—using a concrete example to show how candidates are found and narrowed down. The author argues that understanding how these steps fit together is especially valuable for programmers developing libraries who combine multiple C++ features.

Flap Hero Code Review

AI summaryThis post reviews the source code of Flap Hero, a small C++ game built without an existing game engine, explaining how it is organized using the Plywood framework and how it differs from larger game projects. The author evaluates the deliberate omission of typical engine subsystems—abstract scene representation, a shader manager, a physics engine, and an asset pipeline—rating each choice as OK, Good, or Bad based on the trade-offs involved. Overall, the article suggests Flap Hero is a valuable learning resource for novice and intermediate game developers, though some simplifications (like hand-managed shaders and no asset pipeline) proved disadvantageous.

A Small Open Source Game In C++

AI summaryThe author has released Flap Hero, a free, open source Flappy Bird clone written entirely in C++, with MIT-licensed source code and public domain assets available on GitHub. The game was created to promote Plywood, a module-oriented open source C++ framework (not a game engine) that integrates third-party libraries like Assimp, SoLoud, stb, and GLFW through its workspace and built-in modules. Flap Hero exemplifies the lean, 'handmade' software style Plywood is designed to support, and the author plans future work such as a GUI build manager, with a source code review to follow in the next blog post.

Automatically Detecting Text Encodings in C++

AI summaryThe article describes how the open-source Plywood C++ framework automatically detects text file formats, addressing the inherent ambiguity of encodings like UTF-8, UTF-16 (with or without BOM), and Windows-1252, along with varying line endings. Plywood examines the first 4KB of a file, checks for valid UTF-8 decoding, and scores alternative encodings using heuristics based on whitespace, ASCII characters, and decoding errors. The author benchmarks this approach against a 50-file test suite, noting that editors like Notepad++, Sublime Text, VS Code, and Windows Notepad achieve comparable detection rates, and invites contributions to improve support for formats like Shift JIS on GitHub.

I/O in Plywood

AI summaryThe article introduces the I/O system of Plywood, an open-source C++ framework, positioning it as a modern alternative to the decades-old standard C and C++ runtime I/O libraries. It explains core concepts such as the buffered OutStream and StringWriter classes, adapter-based chaining for newline conversion and character encoding (e.g., writing a UTF-16 file from UTF-8 input), and buffered text reading with InStream and StringReader, illustrated through code examples. The author also highlights design decisions like OutStream objects not being thread-safe, requiring explicit creation via StdOut::binary() or StdOut::text(), and Plywood's general preference for working with UTF-8 text internally.