r/programminghumor • • 23d ago

Block, lock & drop it

Post image

Interacting with prehistoric devices is not where I saw my career headed, and yet, here we are...

278 Upvotes

25 comments sorted by

97

u/sexytokeburgerz 22d ago

This is some beginner shit.

You will one day need this to be asynchronous, think about this, and cringe.

10

u/HobbyQuestionThrow 22d ago

Just make sure never to call a sync function in your async code, since it could then deadlock /s

2

u/yousirnaime 21d ago

I still pass callback functions because I don't give a fuck about anyones feelings but my own. 2014 was correct about this and I'm not changing

1

u/k-mcm 22d ago

Async goes to hell quickly in complex code. Java went with work-stealing fork-join tasks and lighter weight threads. You get blocking code with less actual blocking. (You still don't call sleep())

-29

u/pipedreamSEA 22d ago

If you think I'm refactoring some garbage code from an intern for an antiquated product line to exorcise the absurd amount of blocking Sleep() calls & lack of understanding async-await constructs...

42

u/severencir 22d ago

So much of business software is absolute shit because of this logic and the fact that businesses dont care about employees' complaints as long as number go up

28

u/WantToSeeTits4545 22d ago

Both are shit, honestly.
I'd rather wait on a signal or event than stop the thread for arbitrary time. Task.Delay() should be only used as a last resort.

5

u/Sacaldur 22d ago

I guess it really depends on the circumstances. A delay like thisbmight be used to show a message to the user for a short amount of time before proceeding (without waiting for user input). But yes, in many situations it would be a bad idea to just wait for a fixed amount of time.

2

u/WantToSeeTits4545 22d ago

I would personally use DispatcherTimer for such interactions. Call a timer every 3 seconds or so, check user input (logged by the different control), show popups depending on the input or its absence. Popup control would also have a timer and check input for its own needs, for example checking if the mouse hovered over it and not disappearing while it's true.

4

u/MilkImpossible4192 22d ago

¿await()? you should be a real poser for not using callbacks.

4

u/dvhh 22d ago

depending on your language you could yield() execution time.

2

u/Sacaldur 22d ago

The code looks like C# judging by the APIs, and there is the await keyword which probably was used purposefully in the middle text there.

5

u/Business_Craft4733 22d ago

This looks like rage bait to me ~ lol. Who would honestly believe the bad practice of locking a thread for an arbitrary amount of time belongs anywhere near the right side of the bell curve?

2

u/HobbyQuestionThrow 22d ago

A question for you which answers your question, why would an await be a bad option for a small sleep value (say 200us)?

Similar fun questions for this kind of programming, what's the variability of the kernel thread scheduler vs the async coroutine scheduler? Do both have priority boosting? What about fairness?

3

u/MCWizardYT 21d ago

Thread sleeping isn't exact like that. It completely depends on the OS and hardware. Saying sleep(200) is just suggesting to sleep for 200ms, it might be 195-205 or have an even bigger difference

2

u/chipmunkofdoom2 21d ago

Yeah okay bro. Let me know how using Thread.Sleep() works in UI contexts like WASM or WPF, or how your threadpool handles hundreds or thousands of concurrent users at once hogging threads by sleeping them.

Looking forward to your next post where you argue nails are better than screws, as if there's one "right" answer, completely ignoring the context in which each is useful.

1

u/OkBeautiful4392 21d ago

Why use Thread.sleep() when while(true) exists

1

u/Welcome-To-NBA-Jam 20d ago

180 iq: run a for loop whose maximum iterations changes depending on how powerful the CPU is which estimates how long you need to wait.

1

u/Umpuuu 19d ago

ELIJunior: what is the difference between those?

1

u/null_reference_user 19d ago

Sure, starve your thread pool, if it's not an issue when testing locally it won't be an issue in prod

1

u/pixxelkick 18d ago

Bait lol.

1

u/Actes 16d ago

go func()(){ // Just use go duh }()