TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Why does unsafe multithreaded std:unordered_map crash more than std:map?

92 pointsby luuover 1 year ago

12 comments

ridiculous_fishover 1 year ago
A fascinating design decision of Hashtable in .NET is its support for multiple readers AND a single writer.<p>In C++ or Rust, we get multiple readers OR a single writer. Shared ref or exclusive mut, pick one. But in .NET we have AND not OR: one writer AND multiple readers, all concurrent.<p>This works because .NET&#x27;s GC can tolerate the race. A writer may trigger a table rehash, and a reader will read either the old or new table. If the reader loads the old table, it won&#x27;t be deallocated until it&#x27;s done: the GC will keep it alive.<p>Any cool uses of this one writer AND multiple readers?<p><a href="https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;api&#x2F;system.collections.hashtable" rel="nofollow noreferrer">https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;api&#x2F;system.collecti...</a>
评论 #38203199 未加载
评论 #38202454 未加载
评论 #38202804 未加载
评论 #38205966 未加载
评论 #38201067 未加载
评论 #38203355 未加载
评论 #38201259 未加载
评论 #38201385 未加载
评论 #38202498 未加载
xuhuover 1 year ago
From the article: std::map inserts are O(log n) while std::unordered_map inserts are O(1) and rehashes are O(n). Concurrent rehashes have a high chance of crashing, but I don&#x27;t see how it follows that overall unordered_map crashes more than std::map.<p>Maybe it&#x27;s beacause std::map tree nodes don&#x27;t get reallocated while unordered_map hash buckets do ?
评论 #38201269 未加载
评论 #38199907 未加载
Racing0461over 1 year ago
As soon as i saw microsoft.com, i knew this was gonna be a raymond chen article.
评论 #38203028 未加载
Traubenfuchsover 1 year ago
Crashed a lot... more? You tell me crashing was expected default behaviour to a point where the suitability of implementation details was decided by how much one would crash?
评论 #38204826 未加载
bruce343434over 1 year ago
I wonder why he felt the need to reiterate the obvious here twice, about concurrent read&#x2F;write being the true culprit. I mean, it&#x27;s already obvious from the title that this is just an intellectual pursuit to satisfy some curiosity. Is it really that weird or unconventional of a question?
评论 #38204742 未加载
PessimalDecimalover 1 year ago
What would be a good way to test the author&#x27;s hunch? Just instrument std::unordered_map and trace an execution until a crash occurs?
评论 #38199876 未加载
hindsightbiasover 1 year ago
This article needs a mutex.
评论 #38201687 未加载
kazinatorover 1 year ago
Why does running red lights crash more than driving the wrong way down a one-way street? Discuss among yourselves ...
评论 #38200113 未加载
mathiasgredalover 1 year ago
Anyone making multithreaded code in c++ should be using thread sanitizer. Whether or not a specific datastruture crashes more in multithreaded code will not matter, since tsan will issue a warning on all unsafe accesses.
评论 #38200581 未加载
评论 #38202484 未加载
评论 #38200428 未加载
评论 #38201522 未加载
评论 #38200815 未加载
moregristover 1 year ago
I’m stunned that this article exists and was published by Microsoft. Unsafe access to either is clearly undefined behavior. Discussing which one is more or less undefined is like discussing whether it’s safer to run with scissors or a knife: it doesn’t really matter because you’re going to cut yourself badly and possibly in life-changing ways.<p>Please protect your STL data structures with a mutex. Please keep your thread communications sane. There’s no reason to think through why one is more prone to explosion than another; any conclusions you draw are at best only relevant to your current compiler and STL implementation, and maybe only during some phases of the moon.
评论 #38200688 未加载
评论 #38201484 未加载
评论 #38200718 未加载
评论 #38201678 未加载
评论 #38201011 未加载
评论 #38201688 未加载
评论 #38200870 未加载
评论 #38201446 未加载
评论 #38200812 未加载
xystover 1 year ago
yet another reason to switch to rust
codefloover 1 year ago
Given that “crash” in C++ has a 99% chance to mean “security issue”, and given that I have seen people “pragmatically” go with a solution that “crashes less” in the wild, I’m convinced that nothing good comes out of this line of reasoning.