HelolW rdlo (A threading challenge)

C, 61 62 chars

i;main(){write(1,"Hello World\n"+i++,1);i>13||fork()||main();}

The pthread library functions all have loooooong names, so instead I fired up an entire separate process for each character. fork() is so much shorter.

It was necessary to use write() instead of putchar() because the stdio buffering functions are not thread-safe.

Edited: Back up to 62 chars. In my zeal, dropping down to 61 chars also dropped the thread-safety.


APL (Dyalog) (44 43 39 34)

{⍞←⍺⊣⎕DL⍵}&⌿2 11⍴'Hello World',⍳11

Explanation:

  • 2 11⍴'Hello World',⍳11 creates a matrix: (H,1), (e,2), ...
  • &⌿ means: for each column of the matrix, do on a separate thread:
  • In a thread, is now the character and is now the time
  • ⎕DL⊃⍵ waits for seconds.
  • Then, ⍞←⍺ outputs the character.

Ruby, 46 characters

"Hello World".chars{|c|Thread.new{$><<c}.join}

It is synchronized due to the fact that the program waits for the thread to end before starting the next thread and continuing with the next char.

Tags:

Code Golf