Logo

Programming-Idioms

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

Idiom #25 Send a value to another thread

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

Uses IdThreadSafe; // From Indy
var Queue: TIdThreadSafeStringList;

type

TTask = class(TThread)
  procedure Execute; override;
end;

procedure TTask.Execute;
begin
  while not Terminated do begin
    Sleep(10);
    with Queue.Lock do try
      while Count > 0 do begin
        WriteLn('Hello, ', Strings[0]);
        Delete(0);
      end;
    finally
      Queue.Unlock;
    end;
  end;
end;

begin
  Queue := TIdThreadSafeStringList.Create;
  TTask.Create(False);
  Queue.Add('Alan');
  Sleep(5000);
end. 

It looks like the other implementations use queues. Due to the space limitation, I used the TIdThreadSafeStringList class from Indy for a threadsafe message queue. For standard FP code, use TThreadList with your own TObject based message.
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