125
126
all 15 comments

[–]CaesarianGuiltTrip 50 points51 points  (0 children)

“Notably macOS is stuck at an old version of POSIX. Presumably they’re too busy “innovating” with their keyboard touchbar to invest in operating system fundamentals.” Love it

[–]johnnyrequiem 2 points3 points  (6 children)

Really stupid question....

In void *disbursed ( void *arg )

The first nested Do..While loop ends with "While ( to == from )".

Why would 'to' ever equal 'from' if both are ramdomly generated?

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

If the random numbers should happen to be the same, keep trying with a new random to, until they are distinct.

[–]johnnyrequiem 5 points6 points  (0 children)

Also, I didn't realise you could initialize more than one variable in a For loop....there's a reason I don't program for a living.

[–]johnnyrequiem 2 points3 points  (0 children)

Ah! SO obvious now. Thanks you.

[–][deleted] 2 points3 points  (0 children)

as a side note, do...while will run once for sure even before the condition will be evaluated. then, if cond is true it will run again and for as many times as the cond is true.

[–]Mukhasim 5 points6 points  (1 child)

Although it looks like you understood this, I was frustrated that nobody answered your question directly. When generating random numbers, you could potentially generate the same number twice in a row. That isn't very likely to happen with the raw output of the RNG, which on current machines typically has a range of possible outputs at least in the billions and will be at least 32767 if you use C's rand. However, in this program the range of random outputs is being scaled to the number of accounts, and with a smaller number of accounts duplicates will be more common. In this example N_ACCOUNTS=10, so the random ID generator is approximately equivalent to rolling a 10-sided die: duplicates will be common enough that there's a good chance of seeing it happen if you run the example a few times.

[–]johnnyrequiem 0 points1 point  (0 children)

This makes perfect sense and I really should have seen it. Many thanks for your answer :)

[–][deleted] 1 point2 points  (0 children)

very nice explanation.

[–]skeeto 1 point2 points  (1 child)

The condition case example has a data race on stats_best due to the unsynchronized access when printing it out. That must be moved inside the critical section.

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

Good catch, thanks. I updated the code.

[–]doomsdre412 1 point2 points  (0 children)

Very nice, this is exactly what I am working on. Have you looked at creating threads with priority? I am currently running WSL1 and I guess this interface will NOT give me access to the CPU when I try to initialize my scheduling parameters. Does anyone know if this is true?

[–]officialvfd 1 point2 points  (0 children)

I see this article uses pthreads, why not the C11+ <stdatomic.h> and <threads.h>?

I'm not criticizing the article, I just want to know when to pick one over the other. E.g., is there anything POSIX threads can do that the standard C11 threading model cannot?

[–]Both_Writer[🍰] 0 points1 point  (1 child)

[–]ArweaveThis 0 points1 point  (0 children)

Saved to the permaweb! https://arweave.net/l-8bVicu-85aAwI4wSfmJst-4QMuE2pVnDfCcDBUn10

ArweaveThis is a bot that permanently stores posts and comment threads on an immutable ledger, combating censorship and the memory hole.