Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
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;
#include <future>
#include <mutex>
#include <iostream>
#include <optional>
#include <condition_variable>
//declaration
auto mutex = std::mutex{};
auto cv = std::condition_variable{};
auto variable = std::optional<std::string>{};
auto future = std::async([&]() {
auto lock = std::unique_lock{mutex, std::defer_lock};
cv.wait(lock, [&variable](){ return variable.has_value(); });
std::cout << "Hello, " << *variable << "!" << std::endl;
});
//passing value in main thread (or it can be done in others as well)
{
auto lock = std::unique_lock{mutex};
variable = "Alan";
cv.notify_all();
}
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();
pid = spawn fn ->
receive do
{:hello, name} ->
IO.puts("Hello, #{name}")
_ ->
IO.puts(:stderr, "Unexpected message received")
end
end
send pid, {:hello, "Alan"}
str[1] = "Alan"
sync all
if (this_image() == 1) then
print *,"Hello, ", str
end if
{
// 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
class Process {
volatile String s;
void f() throws InterruptedException {
Thread a = new Thread(() -> {
try {
sleep(1_000);
out.println("Hello, " + s);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
a.start();
sleep(500);
s = "Alan";
}
}
Process p = new Process();
p.f();
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']);
}
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.
queue = Queue.new
thread = Thread.new do
puts queue.pop
end
queue << "Alan"
thread.join