2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

一番簡単な非同期処理(JavaScript/VB.NET/C#/C++/D言語/Ruby/Clojure/HSP)

Last updated at Posted at 2016-12-08

ただ、スレッド立ち上げてスリープして待ち合わせて返り値を受け取る、そんな処理をいくつかの言語で書いてみました。
浅く広く。

JavaScript

Promise<T>のみ

const A=new Promise(
	resolve=>{
		console.log("Promise Start");
		setTimeout(
			()=>resolve("Promise OK!"),
			1000
		);
	}
);

console.log("Waiting...");
A.then(val=>{
	console.log("done!");
	console.log(val);
});

Generatorのみ

const g=(function*(){
	const A=(()=>{
		const gA=(function*(){
			console.log("Generator Start");
			yield setTimeout(()=>gA.next(),1000);
			g.next("Generator OK!");
		})();
		gA.next();
	})();

	console.log("Waiting...");
	A_result=yield A;
	console.log("done!");
	console.log(A_result);
})();
g.next();

Generator & Promise<T>

const g=(function*(){
	const A=new Promise(
		resolve=>{
			console.log("Promise Start");
			setTimeout(
				()=>resolve("Promise OK!"),
				1000
			);
		}
	);

	console.log("Waiting...");
	yield A.then(()=>g.next());
	console.log("done!");
	console.log(yield A.then(val=>g.next(val)));
})();
g.next();

Async/Await(TypeScript & ES.Next)

(async function(){
	const A:Promise<string>=(async function(){
		console.log("Promise Start");
		await new Promise(resolve=>setTimeout(resolve,1000));
		return "Promise OK!";
	})();

	console.log("Waiting...");
	await A;
	console.log("done!");
	console.log(await A);
})();

VB.NET

Task(Of T)

Imports System.Console
Imports System.Threading
Imports System.Threading.Tasks

Module Program
	Sub Main(args() As string)
		Dim A As Task(Of String)=Task.Run(
			Function()
				WriteLine("Task Start")
				Thread.Sleep(1000)
				Return "Task OK!"
			End Function
		)

		WriteLine("Waiting...")
		A.Wait()
		WriteLine("done!")
		WriteLine(A.Result)
	End Sub
End Module

Async/Await

Imports System.Console
Imports System.Threading
Imports System.Threading.Tasks

Module Program
	Sub Main(args() As string)
		Ctype(Async Function()
			Dim A As Task(Of String)=CType(Async Function()
				WriteLine("Task Start")
				Await Task.Delay(1000)
				Return "Task OK!"
			End Function,Func(Of Task(Of String)))()

			WriteLine("Waiting...")
			Await A
			WriteLine("done!")
			WriteLine(Await A)
		End Function,Func(Of Task))().Wait()
	End Sub
End Module

C#

Task<T>

using System;
using System.Threading;
using System.Threading.Tasks;

class Program{
	static void Main(){
		Task<string> A=Task.Run(
			()=>{
				Console.WriteLine("Task Start");
				Thread.Sleep(1000);
				return "Task OK!";
			}
		);

		Console.WriteLine("Waiting...");
		A.Wait();
		Console.WriteLine("done!");
		Console.WriteLine(A.Result);
	}
}

Async/Await

using System;
using System.Threading;
using System.Threading.Tasks;

class Program{
	static void Main(){
		new Func<Task>(async delegate(){
			Task<string> A=new Func<Task<string>>(async ()=>{
				Console.WriteLine("Task Start");
				await Task.Delay(1000);
				return "Task OK!";
			})();

			Console.WriteLine("Waiting...");
			await A;
			Console.WriteLine("done!");
			Console.WriteLine(await A);
		})().Wait();
	}
}

C++

future/async(C++11)

#include <iostream>
#include <thread>
#include <future>
using namespace std;

int main(){
	future<const char *> A=async(launch::async,
		[]{
			cout<<"future Start"<<endl;
			this_thread::sleep_for(chrono::milliseconds(1000));
			return "future OK!";
		}
	);

	cout<<"Waiting..."<<endl;
	A.wait();
	cout<<"done!"<<endl;
	cout<<A.get()<<endl;
}

D言語

core.thread

import std.stdio;
import core.thread;

void main(){
	string A_result;
	Thread A=new Thread(
		delegate(){
			writeln("Thread Start");
			Thread.sleep(dur!"msecs"(1000));
			A_result="Thread OK!";
		}
	);
	A.start;

	writeln("Waiting...");
	A.join;
	writeln("done!");
	writeln(A_result);
}

Ruby

Thread

a_result=""
A=Thread.new do
	puts "Thread Start"
	sleep 1
	a_result="Thread OK!"
end

puts "Waiting..."
A.join
puts "done!"
puts a_result

Clojure

future(CLR版)

(let [A (future
		(println "future Start")
		(System.Threading.Thread/Sleep 1000)
		"future OK!"
	)]

	(println "Waiting...")
	(deref A)
	(println "done!")
	(println (deref A))
)
(shutdown-agents)

HSP

mist

#runtime "hsp3cl"
#include "kernel32.as"
#include "mist.hsp" ;http://fe0km.blog.fc2.com/blog-entry-65.html

mstOpenHspLib
if 0:Sleep 0
mstCaptDll

mstBind "A_result",A_result
mstCompile {"
	#deffunc A
		mes "mstThread Start"
		Sleep 1000
		A_result="mstThread OK!"
	return

	#deffunc mainThread str val
		mes val
	return
"}
mstSetWorkerNum 2
mstThreadCall "A"
mstThreadCall "mainThread","Waiting..."
mstThreadJoin

mes "done!"
mes A_result

実行環境

JavaScript: node v6.9.1
TypeScript: tsc 1.8.10
VB.net: vbc 14.0.1055
C#: csc 4.6.1055.0
C++: cl Version 18.00.40629
D言語: dmd v2.066.1
Ruby: ruby 2.0.0p481
Clojure: Clojure-CLR 1.8.0
HSP: 3.4

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?