use threads; use threads::shared; use strict; use warnings; my $hellos_turn : shared = 1; $| ++; threads->create(sub { while (1) { sleep(1) until ($hellos_turn); print "Hello "; $hellos_turn = 0; threads->yield; } }); threads->create(sub { while (1) { sleep(1) while ($hellos_turn); print "World!\n"; $hellos_turn = 1; threads->yield; } }); ;