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.

The case of the critical section that let multiple threads enter a block of code

140 pointsby luuabout 2 months ago

10 comments

akoboldfryingabout 2 months ago
Loose typing strikes again.<p>I understand the temptation of &quot;Let&#x27;s just use an int return type, that way later on we can easily let them indicate different flavours of error if we want&quot;. But then you leave yourself open to this.<p>The full-BDSM approach is for every component Foo that works with callbacks like this to define its <i>own</i> FooResult type with no automatic type conversions <i>to</i> it. In C, enum suffices, though a determined idiot can just cast to override; using FooResult as defined below makes it harder to accidentally do the wrong thing:<p><pre><code> enum FooResultImpl { FOO_SUCCESS_IMPL, FOO_FAILURE_IMPL }; struct FooResult { enum FooResultImpl USE_A_CONVERSION_FUNCTION_INSTEAD_OF_ACCESSING_THIS_DIRECTLY; } FOO_SUCCESS = { FOO_SUCCESS_IMPL }, FOO_FAILURE = { FOO_FAILURE_IMPL }; </code></pre> (Yes, it&#x27;s still possible to get an erroneous value in there at initialisation time without having to type out &quot;USE_A_CONV...&quot; -- but this is C, you can only do so much. You can&#x27;t even prevent a FooResult variable from containing uninitialised garbage...)
评论 #43452577 未加载
评论 #43452848 未加载
robmccollabout 2 months ago
Looking at Microsoft&#x27;s C code makes my eyes hurt. I don&#x27;t know if it&#x27;s the style (bracket placement, no new lines), naming conventions, typedeffing away pointers, or what, but it just doesn&#x27;t read easily to me.
评论 #43453912 未加载
评论 #43452936 未加载
评论 #43454952 未加载
评论 #43453521 未加载
评论 #43453223 未加载
davydmabout 2 months ago
Or rather, &quot;the case of a buggy lazy-init function which reinitialized the critical section every time&quot;
评论 #43451637 未加载
putzdownabout 2 months ago
I wake up every morning and thank God I am not working on or near Microsoft code. There is nothing about this code or anything about this story that is in any way sensible or pleasing. Take a simple, well-solved problem. Forgot all prior solutions. Solve it badly, with bad systems and bad ideas. Write the code in the ugliest, most opaque, most brittle and fragile manner imaginable. Now sit back and enjoy the satisfaction of getting to debug and resolve problems that never should have happened in the first place. The miracle is that Microsoft, built as it is to such a degree on this kind of trashy thinking and trashy source, still makes its annual billions. That right there is the power of incumbents.
tialaramexabout 2 months ago
I think I don&#x27;t understand why they&#x27;re making a critical section at all.<p>The end goal is to initialize something no more than once, right? But the technology they&#x27;re using (wrongly, but it did exist and they were clearly aware of it) to make a critical section <i>does</i> initialize a thing exactly once.<p>I also don&#x27;t understand the use of SRWLock here, or rather, I sort of do but it&#x27;s a hole in Microsoft&#x27;s technology stack. SRWLock is a complicated (and as it turns out, buggy, but that&#x27;s not relevant here) tool, but all we want here is a mutex, so we don&#x27;t actually need SRWLock except that Microsoft doesn&#x27;t provide the simple mutex, so this really is what you&#x27;d have written at the time† - conjure into existence the over-complicated SRWLock and just don&#x27;t use most of its functionality.<p>† Today you should do the same trick as a Linux futex although you spell it differently in Windows. It&#x27;s also optionally smaller, which is nice for this sort of job, a futex costs 4 bytes but in Windows we can spend just one byte.
评论 #43454795 未加载
评论 #43452963 未加载
评论 #43452893 未加载
评论 #43452536 未加载
评论 #43453198 未加载
评论 #43452507 未加载
hyperhelloabout 2 months ago
It looks Windows is lousy with callbacks and APIs that put the burden of understanding everything on the user, and some of Windows uses 0 to mean success, and some of Windows doesn&#x27;t.
评论 #43453799 未加载
评论 #43453591 未加载
评论 #43457006 未加载
评论 #43456436 未加载
评论 #43452906 未加载
评论 #43455347 未加载
xyzzy9563about 2 months ago
Just use strong typing and mutexes. This isn&#x27;t rocket science.
vijaybrittoabout 2 months ago
I have a naive question here.<p>Could this have been avoided if they had used Rust? Or is this a bug that can happen even in Rust code too?
评论 #43461489 未加载
评论 #43471155 未加载
commandlinefanabout 2 months ago
I didn&#x27;t even have to click through the article to know that it would be Raymond Chen ; )
ddtaylorabout 2 months ago
Microsoft take note that I read this article and everything Raymond Chen puts out under your company brand. I have zero interest in Windows as a platform and actively steer large customers away from it anytime it&#x27;s discussed, since it has no value offering for most of us.
评论 #43456486 未加载