Concurrent Data Structures

Thread-safe data structures: concurrent hash maps, skip lists, queues, stacks, read-write locks, lock-free algorithms, and compare-and-swap patterns.

The data

Structures

NameTypeDescriptionImplementationsContentionNotesOperations
Concurrent Hash Map (Striped/Segmented)Lock-basedHash map with segmented locking: different buckets can be locked independently, allowing concurrent access to different segments
  • Java ConcurrentHashMap (striped, CAS-based)
  • C++ tbb::concurrent_unordered_map
  • .NET ConcurrentDictionary
Low (only on same segment)Java 8+ uses CAS + synchronized on individual bins; read operations often lock-freenull
Concurrent Skip ListLock-free (optimistic)Probabilistic balanced search structure; uses CAS for lock-free insertion/deletion with high probability
  • Java ConcurrentSkipListMap/Set
  • C++ concurrent_skiplist
nullBetter concurrency than balanced trees (no rebalancing); used as concurrent sorted mapO(log n) expected for search/insert/delete
Lock-Free Queue (Michael-Scott)Lock-freeLinked-list queue using CAS on head and tail pointers; multiple producers/consumers without locks
  • Java ConcurrentLinkedQueue
  • C++ boost::lockfree::queue
nullMay have memory reclamation issues (ABA problem); use hazard pointers or epoch-based reclamation in productionO(1) amortized enqueue/dequeue
Concurrent Stack (Treiber Stack)Lock-freeLinked-list stack using CAS on top pointer; simple but can have contention on single atomic variable
  • C++ tbb::concurrent_bounded_queue (similar)
  • Java ConcurrentLinkedDeque
nullHigh contention under heavy concurrent use; consider combining techniques or elimination backoffO(1) push/pop
Read-Write LockLock-based (shared/exclusive)Allows multiple concurrent readers OR one exclusive writer; optimizes read-heavy workloads
  • Java ReentrantReadWriteLock
  • C++ shared_mutex (C++17)
  • POSIX pthread_rwlock_t
nullRisk of writer starvation under heavy read load; use StampedLock (Java) or upgradeable read locksRead: O(1), Write: O(1) + potential writer starvation
Compare-And-Swap (CAS)Atomic primitiveAtomically: if (location == expected) location = new_value; return success/failure. Foundation of all lock-free algorithms
  • Java Unsafe.compareAndSwap*()
  • C++ std::atomic<T>::compare_exchange_weak/strong
  • x86 CMPXCHG instruction
nullCAS loop pattern: while (!CAS(loc, expected, new)) expected = *loc; ABA problem requires versioned pointers or hazard pointersO(1) atomic
Hazard PointersMemory reclamationLock-free memory reclamation technique: threads announce which nodes they're accessing; nodes not in any hazard list can be safely freed
  • Boost.Lockfree
  • Folly (Facebook)
  • DPDK
nullSolves ABA and use-after-free in lock-free structures; bounded memory overhead (O(threads × hazard_slots))O(1) publish/unpublish
Epoch-Based ReclamationMemory reclamationThreads announce their epoch; retired nodes are freed when all threads have moved past the epoch when node was retired
  • Seastar
  • Folly
  • RCU (Read-Copy-Update) in Linux kernel
nullLower overhead than hazard pointers for read-heavy workloads; used extensively in kernel RCUO(1) enter/exit epoch

Fetch the same bytes

The static files are identical to what the API returns, but with no rate limit and no server round trip. Use the API when you want a query and a content type; use the files when you want to cache one document.

curl "https://yjtoon.com/api/dataset/concurrent-data-structures?format=toon"
const res = await fetch(
  "https://yjtoon.com/static-data/dataset/concurrent-data-structures.toon"
);
const toon = await res.text();

Rate limit: 120 requests per minute per IP, no key and no signup. API reference →

Topics

  • concurrency
  • lock-free
  • compare-and-swap
  • concurrent-hash-map
  • concurrent-queue
  • read-write-lock
  • atomic