Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Perl

Idiom #25 Send a value to another thread

Share the string value "Alan" with an existing running process which will then display "Hello, Alan"

use threads;
use threads::shared;
my @queue :shared;

threads->create('my_task');
push @queue, 'Alan';
sleep 5;

sub my_task {
   while (1) {
      while (@queue) {
         print "Hello, ", (pop @queue), "\n";
      }
      sleep 1;
}

         
      

We sleep so that the thread can print the greeting before the program ends.
declare

   task Greeter is
      entry Greet (Name : String);
   end Greeter;

   task body Greeter is
   begin

      accept Greet (Name : String) do
         Ada.Text_IO.Put_Line ("Hello, " & Name);
      end Greet;

   end Greeter;

begin
   Greeter.Greet ("Alan");
end;

This is called a rendez-vous in Ada. The caller is blocked until the Greet accept statement is completed.

New implementation...
< >
programming-idioms.org