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

Idiom #25 Send a value to another thread

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

ch <- "Alan"
using System.Collections.Concurrent;
using System.Threading;
ConcurrentQueue<int> coll = new ConcurrentQueue<int>();
var ts = new CancellationTokenSource();
CancellationToken ct = ts.Token;

Task t2 = Task.Factory.StartNew(() => {
	while (true) {
		Thread.Sleep(250);
		if (ct.IsCancellationRequested) break;
			bool isDequeued = coll.TryDequeue(out int result);
			if (isDequeued) Console.WriteLine(result);
	}});
while (true) {
	int val = Convert.ToInt32(Console.ReadLine());
	if (val == -1) break;
	coll.Enqueue(val);
}
ts.Cancel();
import std.concurrency;
import std.stdio;
auto t = spawn( {
   receive( (string s) { writefln("Hello, %s", s); } );
} );

t.send("Alan");
import "dart:isolate";
sendPort.send("value");
str[1] = "Alan"
sync all
if (this_image() == 1) then
  print *,"Hello, ", str
end if
import Control.Concurrent.Chan
writeChan peopleToGreet "Alan"
{
  // in file worker.js
  onmessage = ({data}) => {
    console.log (`Hello, ${data}`)
  }
}
{
  // in file main.js
  const worker = new Worker ('worker.js')
  worker.postMessage ('Alan')
}
https://gist.github.com/f7c174c53efaba4d8575
extension=pthreads.so
class AsyncOperation extends Thread
{
    private $name;

    public function __construct($arg)
    {
        $this->name = $arg;
    }

    public function run()
    {
        sleep(1);
        if ($this->name) {
            printf("Hello %s\n", $this->name);
        }
    }
}

$thread = new AsyncOperation("World");

if ($thread->start()) {
    $thread->merge(['name' => '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. 
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;
}

         
      
import Queue
q = Queue()

t = Thread(target=worker)
t.daemon = True
t.start()

q.put("Alan")
queue = Queue.new
thread = Thread.new do
  puts queue.pop
end
queue << "Alan"
thread.join
use std::thread;
use std::sync::mpsc::channel;
let (send,recv)=channel();

thread::spawn(move||
{  
    loop
    {  
        let msg=recv.recv().unwrap();
        println!("Hello, {:?}",msg);
    }  
});

send.send("Alan").unwrap();

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org