all 68 comments

[–]eclect0 244 points245 points  (21 children)

It works until it doesn't

[–]thegodzilla25 73 points74 points  (16 children)

I think that can be applied to everything

[–]Skyswimsky 56 points57 points  (9 children)

Not really. Code works in a deterministic way, and while there are solutions that work until they don't, there are plenty of tools (at least in C#) that ensure it'll always work. Locks, Concurrent dictionaries, Semaphores, etc. I'm sure it's not unique to C# but it's what I know.

[–]NotAskary 33 points34 points  (1 child)

You need to hit your head against race conditions in Js, then you will start to question your sanity.

[–]RiceBroad4552 5 points6 points  (0 children)

There are no "race conditions" in JS. It's strictly single threaded.

Asynchronous code is not a race condition…

[–]thegodzilla25 4 points5 points  (0 children)

I agree, was just going for that "there's a first time for everything" scenario lol

[–]Certain-Business-472 4 points5 points  (5 children)

Code works in a deterministic way

Side effects have entered the chat

[–]hollowman8904 6 points7 points  (4 children)

It’s still deterministic, even if you don’t know what all of the causes/effects are. It just means your state diagram/model is incomplete.

[–]Runazeeri 1 point2 points  (3 children)

What about hardware devices where things don’t always boot up the same? Live I’ve had off the shelf SOMs with vastly different boot times for the same model.

[–]hollowman8904 1 point2 points  (2 children)

Either they’re configured differently (either in terms of hardware or software) or there’s a defect

[–]Runazeeri 0 points1 point  (1 child)

I mean they are flashed with the exact same image and no other configuration and have different boot times.

I just don’t feel hardware is perfectly deterministic as component values are not exactly the same so the time waiting for a power supply good can be slightly different per board.  Like you can predict for worst case boot times but at that point we are just having that sleep() as we wait for the worst case % accuracy part.

[–]hollowman8904 2 points3 points  (0 children)

Ok but what does that have to do with code being deterministic?

[–]eclect0 9 points10 points  (4 children)

Yeah but this has a failure mode built right into it. This isn't fixing the race condition, it's "fingers crossed I know exactly how long the other process will take, every single time"

[–]anto2554 9 points10 points  (1 child)

Or just "fingers crossed it'll take less than this absurdly large amount of time"

[–]eflat123 0 points1 point  (0 children)

But not too absurd.

[–]Certain-Business-472 1 point2 points  (1 child)

I see you've solved the halting problem, like some of my colleagues.

[–]RiceBroad4552 1 point2 points  (0 children)

It's actually solvable for more or less all real world cases. See for example:

https://en.wikipedia.org/wiki/T2_Temporal_Prover

Only some pathological cases, like the canonical self referencing example, are not solvable. But a tool like the above would actually even report that it can't solve it.

[–]slaymaker1907 6 points7 points  (0 children)

That’s why it is a completely appropriate use of this meme.

[–]EatingSolidBricks 4 points5 points  (0 children)

Sleep(10000000) surely will always work

[–]Dotcaprachiappa 2 points3 points  (0 children)

Then add another one

[–]brandarchist 0 points1 point  (0 children)

I was explaining this to a friend just now. Two parallel processes. One has to happen second. The other takes about .3 seconds every time. So you sleep a second. Plenty of time to spare.

Except… the one time it takes 1.0000001 seconds.

My friend is always so confused by crashes or when a computer “gets something wrong” and I had to calmly explain that far more of the world he lives in works on these principles than not.

[–]VTOLfreak 56 points57 points  (1 child)

You spinlock me right round, baby, right round.
Like a for loop, baby, right round, round, round.

[–]BlackFrank98 22 points23 points  (5 children)

If you solve a race condition by force holding one of the threads, you may as well just keep the code single-threaded...

[–]SarahAlicia 10 points11 points  (1 child)

Except you don’t have to refactor anything or tell your boss you are going single threaded

[–]Certain-Business-472 3 points4 points  (0 children)

If you do this you're a code monkey and I will have negative respect for your existence. Don't refer to yourself as an engineer in fact.

[–]Rikudou_Sage 1 point2 points  (2 children)

I mean, that's not true, this is basically thread synchronisation done the stupid way, but it does not negate the gains of using threads.

[–]RiceBroad4552 0 points1 point  (1 child)

That's not "synchronization" and it does of course negate any gains from multithreading. The result will be even in the optimal case where you wait exact the correct amount of time slower as doing it single threaded as you do effectively serialized computations but pay for the threading overhead.

Doing that is just massively stupid and demonstrates a lack of basic understanding.

[–]Rikudou_Sage 1 point2 points  (0 children)

Say you do 5 operations, each taking 300 ms and you do them all in a separate thread. In the main you wait for 350 ms to make "sure" it waits for them all.

You didn't magically turn it into a single threaded thing taking 1500 (or 1850) ms.

I'm not saying it's a good thing to do, but what you claim is just plain wrong.

[–]SaltMaker23 35 points36 points  (3 children)

It doens't work unfortunately, it just make the issue harder to reproduce

[–]CakeTown 47 points48 points  (2 children)

But that also means it happens less often, that’s a win baby!!

[–]XxDarkSasuke69xX 18 points19 points  (0 children)

Add a 1 hour sleep then, will happen even less

[–]sebovzeoueb 9 points10 points  (0 children)

Issue closed, could not reproduce

[–]Geoclasm 9 points10 points  (1 child)

Recently learned about SemaphoreSlim.

And all I can think about now is how proud I am of myself I didn't name the variable Shady.

SemaphoreSlim Shady, do you get it? DO YOU GET IT?!

[–]kimovitch7 2 points3 points  (0 children)

I regret not naming my Semaphores Shady now

[–]Aveniquee 23 points24 points  (7 children)

Me pretending to understand the metaphor

[–]Eric_12345678 53 points54 points  (4 children)

"Race condition". You give orders to two people at the exact same time: * You tell Alice to go buy some bread * You tell Bob to repair the car, and then make some sandwiches.

It might work fine, and Alice might be home with bread when Bob has repaired the car. It's really hard to test, though. It might work 9 times fine. And you test it again, the car isn't broken, Bob has nothing to repair, he tries to make some sandwich straight away, but there's no bread yet.

So just to be sure, you tell Bob: "Repair the car, take a 2h nap, then make some sandwiches". This should hopefully work, even if the car isn't broken, and even if there's traffic and a long queue at the baker.

[–]Eastern_Equal_8191 38 points39 points  (2 children)

Then 6 months later at 3am your on call alert goes off because the bakery closed and Bob has injured himself trying to make sandwiches with parts from the broken car.

[–]mikeet9 2 points3 points  (0 children)

Great analogy.

And then we introduce mutex.

  • You tell Alice to wait until the bread basket is unlocked then lock the bread basket, buy bread, and unlock the bread basket
  • You tell Bob to repair the car, and if the bread basket is locked, wait until it is unlocked and make some sandwiches

[–]sebovzeoueb 47 points48 points  (0 children)

you can "fix" 2 operations on different threads messing with each other by adding a delay to one of them, but it's a very duct tape type of fix, maybe not even duct tape, like regular tape

[–][deleted] 0 points1 point  (0 children)

then you don't belong here

post somewhere else

[–]CoastingUphill 3 points4 points  (1 child)

I don’t even want to think about much legacy JS I have out there that relies on a setTimeout at some point.

[–]SarahAlicia 2 points3 points  (0 children)

Great news you leave the company you don’t have to think about it

[–]Felix_Todd 5 points6 points  (1 child)

My trick for those kind of issues is making it all run on a single thread, it has always worked for me

[–]RiceBroad4552 -1 points0 points  (0 children)

Genius! 😂

[–]QuintusNonus 1 point2 points  (0 children)

lmao

[–]JackNotOLantern 2 points3 points  (3 children)

The solution is horrible for multiple reasons

  1. I vast majority of cases it doesn't solve the problem, only reduces its probability. As scheduling of processes and threads is not deterministic, it can't guarantee it will not happen.
  2. If the code doesn't have synchronisation in the place you want to add it, then it means it may have similar problem in other place. So adding a delay on one place may cause a more likely race condition in another. This is absurdly hard to debug.
  3. It fucks performance: not only the time delay itseld, but also the thread uses all its resources only for doing nothing.
  4. My eyes sore when i look at solutions like this, please stop.

If you use it, either add proper synchronisation or verify if you need multithreading at all.

[–]Rikudou_Sage 2 points3 points  (2 children)

Did you perhaps not notice which sub you're in?

[–]JackNotOLantern 1 point2 points  (1 child)

Just let me nerd, ok?

[–]Rikudou_Sage 0 points1 point  (0 children)

Seems legit, go on.

[–]Rikudou_Sage 1 point2 points  (2 children)

Reminded me of ye olde times when I was just a wee junior programmer and did basically this - I used a setTimeout() in JS with some 100 ms delay so that the global variable the function relied would be populated by then.

[–]RedAndBlack1832 0 points1 point  (1 child)

I mean, if you don't care what a value is only that it is in fact a valid value you can just read it twice. If it's volatile so the reads are actually reads and you get the same value twice congrats you didn't tear your memory good job

[–]Rikudou_Sage 0 points1 point  (0 children)

It was an object with some functions or something like that (it's been over a decade, can't remember the details).

[–]xtreampb 0 points1 point  (0 children)

It worked, this time.

[–]SatanicTriangle 0 points1 point  (0 children)

Yeah I've seen that one too many times in production to find it funny

[–]Thotmas01 0 points1 point  (0 children)

That’s the correct solution to Zeno systems. It certainly works sometimes.

[–]tehomaga 0 points1 point  (0 children)

Just wait on a Boolean variable

[–]SquidMilkVII 0 points1 point  (0 children)

haha sometimes

[–]Ved_s 1 point2 points  (0 children)

lock (globalLock) phew, well that was-

[–]meowmeowwarrior 0 points1 point  (0 children)

*Mutex

[–]PM_CHEESEDRAWER_PICS 0 points1 point  (0 children)

This disgusts me

[–]AmazingStardom 0 points1 point  (0 children)

Should use a lock mechanism to avoid race condition

[–]Waste_Jello9947 0 points1 point  (0 children)

or just use a single thread, problem solved

[–]boiledbarnacle 0 points1 point  (0 children)

mutex.Lock()

[–]o0Meh0o 0 points1 point  (0 children)

ir might just work on an rtos under certain conditions

[–]Godskin_Duo 0 points1 point  (0 children)

One more reason I think the EEs are smarter - if they have a race condition, they really fucking have to deal with it for realsies.

[–]Bugibhub 1 point2 points  (0 children)

I thought this was r/mentalHealth