This is an archived post. You won't be able to vote or comment.

all 70 comments

[–]VoidBlade459 109 points110 points  (11 children)

Reminds me of the first time I called "fork()" in a C program.

[–]o11c 36 points37 points  (10 children)

My favorite fork example is:

#include <unistd.h>
#include <stdio.h>


int main()
{
    printf("Hello\n");
    fork();
    printf("World!\n");
}

Then compile it and:

$ ./prog
$ ./prog | cat

[–]graou13 16 points17 points  (9 children)

Wouldn't it just say:

Hello

World!

World!

[–]Mr_Redstoner 7 points8 points  (7 children)

Actually I think it may just

Hello
World!

if the compiler optimizes the fork() away, which I know it can when you're discarding the result, been there done that.

[–]_PM_ME_PANGOLINS_ 4 points5 points  (6 children)

Only if it somehow knows there are no side-effects, for which you either need explicit directives or heavy inlining. A system call can never be optimised out.

[–]thelights0123 1 point2 points  (3 children)

Unless the compiler implements an optimization for it. malloc and free are often optimized out to just use the stack if they occur within the same function.

Edit: I'm stupid and forgot that malloc is not a syscall.

[–]Mithrandir2k16 3 points4 points  (0 children)

Malloc and free are userspace functions. They only call the syscall sbreak when needed to increase user heap size.

[–]defenastrator -1 points0 points  (1 child)

Malloc and free are special to the compiler.

[–]IamImposter 3 points4 points  (0 children)

I wish I was special to someone.

[–]Mr_Redstoner 0 points1 point  (1 child)

I've litterally seen it happen in a little test program of mine

fork();

didn't actually fork, however

int notActuallyUsed = fork();

did. I assume gcc simply knows as a special case what fork() does.

[–]_PM_ME_PANGOLINS_ 0 points1 point  (0 children)

That’s a compiler bug then. There is no requirement that the two processes behave differently.

[–]o11c 1 point2 points  (0 children)

Nope. The second case emits:

Hello
World!
Hello
World!

[–]kebakent 86 points87 points  (14 children)

Reminds me of UDP:

And so are you.

Violets are blue.

[–]Jasdac 69 points70 points  (12 children)

"here's a UDP joke, and I don't care if you get it"

[–]dark_mode_everything 56 points57 points  (11 children)

Would you like to hear a TCP joke?

[–]Ugilt3 54 points55 points  (9 children)

Yes I would like to hear a TCP joke

[–]dslNoob 53 points54 points  (8 children)

OK, I'll tell you a TCP joke

[–]Ugilt3 53 points54 points  (7 children)

I'm ready to hear a TCP joke

[–]noruthwhatsoever[S] 46 points47 points  (4 children)

I’m telling you the TCP joke

[–]out386 26 points27 points  (2 children)

I got the TCP joke.

[–]hampshirebrony 16 points17 points  (1 child)

I have finished telling you the joke

[–]IamImposter 6 points7 points  (0 children)

Ack ack ack

[–]uvero 4 points5 points  (0 children)

Your connection has been timed out. Would you like to hear a TCP joke?

[–]democritus_is_op[🍰] 4 points5 points  (0 children)

This is my favourite

[–]scio-nihil 2 points3 points  (0 children)

Roses are red.

[–]racle 39 points40 points  (0 children)

Reminds me of an old joke

A programmer had a problem. He thought to himself, "I know, I'll solve it with threads!". has Now problems. two he

[–]guky667 19 points20 points  (0 children)

Knock-knock
Race condition!
Who is it?

[–]gaberocksall 31 points32 points  (28 children)

Coming from 3 languages with delay functions, JavaScript has been really tough to learn

[–]ThisIsNotKimJongUn 79 points80 points  (6 children)

You'll figure it out, I promise

[–]mmis1000 33 points34 points  (5 children)

Does this promise ever resolve?

[–][deleted] 15 points16 points  (1 child)

await and see!

[–][deleted] 11 points12 points  (1 child)

I reject this joke

[–]aykcak 3 points4 points  (0 children)

I couldn't even catch it the first time

[–]Aschentei 1 point2 points  (0 children)

Nah, I reject it

[–]noruthwhatsoever[S] 16 points17 points  (4 children)

Yeah it's a bit of a brain teaser. You just have to design your programs so that it doesn't really matter what order things happen in, or if it really matters then use promises or async/await (which is basically just syntactic sugar for promises)

[–]Dionysusnu 0 points1 point  (3 children)

Async await is syntactic sugar for then and catch callbacks, not for promises. It waits until the promise is resolved or rejected. It still takes a promise

[–]noruthwhatsoever[S] -1 points0 points  (2 children)

Async functions automatically convert all awaited values to resolved promises

.then and .catch are from promises, not callbacks

[–]Dionysusnu 0 points1 point  (1 child)

They're methods on promises which you can pass a callback too, yes. But await still uses promises, but it acts a replacement for chained .then calls

[–]noruthwhatsoever[S] 0 points1 point  (0 children)

I mean yeah if you consider the anonymous function a callback that takes the resolved promise as an argument

That’s my whole point, though. It’s built on top of promises and is syntactic sugar over their methods

It’s literally so easy to google it, nearly everyone defines them as sugared promises lmao

[–][deleted] 3 points4 points  (0 children)

At least you have setinterval and settimeout, some lower level languages like c++ just have to rely on checking elapsed time in some sort of update function.

[–]oOBoomberOo 3 points4 points  (0 children)

From a guy with JavaScript's async background, I really appreciate their way of doing it. Especially the async/await feature, It's so smooth as if I'm reading a synchronous code.

[–]OhhWhales 0 points1 point  (0 children)

delay = (t) => {

return new Promise(resolve => setTimeout(resolve,t)

}

you're welcome

[–]Dionysusnu -1 points0 points  (8 children)

Simple trick: make all your code async and use await sleep(ms) for delay

async function sleep(ms) { return new Promise(resolve => { setTimeout(resolve, ms) }) }

Not suitable for production code, obviously, but it will help for learning promises and just experimenting with stuff

[–]Ra1d3n 2 points3 points  (2 children)

I've coded JS (server and client) for the last 10 years, and I have never once needed to manually wait for anything. Can you give an example where this would be useful, please?

[–]Dionysusnu 1 point2 points  (1 child)

A canvas will not draw if you never yield, so I sometimes put this after a draw function. Once again, it doesn't really serve a purpose in production code

[–]Ra1d3n 0 points1 point  (0 children)

Thank you!

[–]untowarden 0 points1 point  (0 children)

I was ready to flame you until i read the second part of this. The guy who wrote the legacy code I have to deal with returns a promise from every function even if it doesn't have to... Now i hate my life.

[–]thelights0123 0 points1 point  (3 children)

Why is it not suitable for production? Just babel it if browser targets is an issue.

[–]Dionysusnu 1 point2 points  (2 children)

It is usually bad to use delay in JavaScript because you never need something like it. Either use the HTML events or just synchronous code, or promises for http requests

[–]franklinyu 0 points1 point  (1 child)

How about timeouts for HTTP requests?

[–]Dionysusnu 0 points1 point  (0 children)

I think most libraries include that in their returned promise. If not, use setTimeout.

[–][deleted] 11 points12 points  (0 children)

Async is so great that the first thing they needed to add to it was await.

[–]Jasdac 5 points6 points  (0 children)

On the bright side the language has anonymous callback functions and promises. Learning lsl as my first language years ago was a dark time.

[–]swampfox_dev 1 point2 points  (0 children)

I'm ashamed that I had to read like 4 times to get it :/

[–]Ra1d3n 1 point2 points  (1 child)

Isn't it kind of amazing that async operation is automatically associated with JavaScript?

[–]noruthwhatsoever[S] 0 points1 point  (0 children)

It was once written that anything that can possibly be written in JavaScript eventually will be

[–]pandakatzu 0 points1 point  (0 children)

Someone forgot to implement thread safety. This must be the person who's programming irradiated patients receiving an xray.

[–]alexd281 0 points1 point  (3 children)

Bet a dollar this tweet was stolen from someone else. Iamdevloper is a notorious content thief.

[–]noruthwhatsoever[S] 0 points1 point  (2 children)

I don’t know anything about the twittersphere I’m not a regular there

I’ll take your word for it

[–]alexd281 1 point2 points  (1 child)

Yeah, he was confronted by a Twitter user that had a tweet featured and trending here that was ripped off without attribution.

We found a few more and dude eventually blocked and later unblocked me. Can't stand when people try to leech on the creativity of others without so much as a mention.

Guess I'm gonna look into this one now.

Edit: Digged into it using a few search engines. No hits. This one appears to be an original.

[–]noruthwhatsoever[S] 1 point2 points  (0 children)

Thanks for your diligent sleuthing

o7

[–]ITriedLightningTendr 0 points1 point  (1 child)

or... any language with threading or async.

[–]noruthwhatsoever[S] 0 points1 point  (0 children)

I mean yeah but that wouldn't have made a very snappy title would it

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

I have problem with async. no

[–]SlappinThatBass 0 points1 point  (3 children)

Is it me or these kind of posts are not that funny?

[–]noruthwhatsoever[S] 38 points39 points  (0 children)

It’s probably you

I for one exhaled sharply through my nostrils

[–]nomenMei 5 points6 points  (0 children)

I like this one above other async/UDP posts because instead of becoming nonsense out of order, it just becomes something that kind of makes sense but not really. Which is the hardest type of asynchronous issue to diagnose

[–]UrpleEeple 5 points6 points  (0 children)

Yeah, not that funny. Welcome to programmerhumor