Rustのタスクモデル
@dim7th
9/7 Rust Samurai その弐
13年9月7日土曜日
•学芸大学教育学部2年
•Twitter @dim7th
•3Dプリンタとか
•scheme, python, C++, Dartとか
自己紹介
13年9月7日土曜日
1.タスクについて
2.タスクの通信
3.immutableなデータの共有
Rustのタスクモデルについて
13年9月7日土曜日
Rust 0.8-preで話します
• RustTasks and CommunicationTutorial
• http://static.rust-lang.org/doc/tutorial-tasks.html
13年9月7日土曜日
1.タスクについて
13年9月7日土曜日
Rustにおけるタスクとは
• VMにおけるグリーンスレッドのようなもの
• 軽量でタスク間においてはメモリが独立
• タスク間のメッセージ通信が可能
• OSのスレッドにRustのスケジューラが割り当てる
• マルチコアであれば並列に割り振られる
13年9月7日土曜日
というわけで実際に
13年9月7日土曜日
タスクを生成してみる
fn main(){
spawn( || println("hello"));
}
13年9月7日土曜日
fn main(){
spawn( || println("hello"));
}
Lambda Expression
タスクを生成してみる
13年9月7日土曜日
Lambda Expression
let func = | arg | {
println(fmt!(“arg=%?”,arg));
}
func(10)
=>10
13年9月7日土曜日
よく使われる形で
do spawn() {
println(“hello, world!”);
}
13年9月7日土曜日
よく使われる形で
do spawn() {
println(“hello, world!”);
}
Do Expression
13年9月7日土曜日
spawn( |arg| {
hoge(arg) ;
});
Do Expression
13年9月7日土曜日
Do Expression
spawn( |arg| {
hoge(arg) ;
});
do spawn |arg| {
hoge(arg);
}
13年9月7日土曜日
2.タスクの通信
13年9月7日土曜日
Pipe
13年9月7日土曜日
“A pipe is simply
a pair of endpoints”
13年9月7日土曜日
Pipe
13年9月7日土曜日
portchan
Pipe
13年9月7日土曜日
Pipe
Task
Channel
Port
AnotherTask
13年9月7日土曜日
実際にやってみる
use std : : task;
let (port , chan) : (Port<int>, Chan<int>) = stream( );
do spawn | arg | {
chan . send (arg);
}
13年9月7日土曜日
実際にやってみる
use std : : task;
let (port , chan) : (Port<int>, Chan<int>) = stream( );
do spawn | arg | {
chan . send (arg);
}
型宣言
13年9月7日土曜日
実際にやってみる
use std : : task;
let (port , chan) : (Port<int>, Chan<int>) = stream( );
do spawn | arg | {
chan . send (arg);
}
型宣言
argはport . recv()で取得できる
13年9月7日土曜日
一つのChanは複数の
タスクから使えない
13年9月7日土曜日
例えば
let (port , chan) = stream( );
do spawn | arg1 | {
chan . send (arg1);
}
do spawn | arg2 | {
chan . send (arg2);
}
13年9月7日土曜日
`chan` moved into closure
environment here
because it has type `~fn:Send( )`,
which is non-copyable
(perhaps you meant to use clone( )? )
error: aborting due to previous error
13年9月7日土曜日
怒られます
13年9月7日土曜日
そこで出てくるのが
13年9月7日土曜日
SharedChan
13年9月7日土曜日
use std : : comm;
let (port , chan) = comm : : stream( );
13年9月7日土曜日
use std : : comm;
let (port , chan) = comm : : stream( );
let chan = comm : : SharedChan : : new(chan);
13年9月7日土曜日
use std : : comm;
let (port , chan) = comm : : stream( );
let chan = comm : : SharedChan : : new(chan);
chanを上書きする
13年9月7日土曜日
use std : : comm;
let (port , chan) = comm : : stream( );
let chan = comm : : SharedChan : : new(chan);
do spawn | hoge | {
let child_chan = chan . clone( );
child_chan.send(arg);
}
13年9月7日土曜日
use std : : comm;
let (port , chan) = comm : : stream( );
let chan = comm : : SharedChan : : new(chan);
do spawn | hoge | {
let child_chan = chan . clone( );
child_chan.send(arg);
}
タスク内でclone( )する
13年9月7日土曜日
Servoではどうなの?
servo/src/components/main/servo.rc
151行目らへん
13年9月7日土曜日
3. immutableなデータの共有
13年9月7日土曜日
Pipeを使ってもできる
13年9月7日土曜日
Pipeを使ってもできる
immutableなので
無駄なデータのコピー
13年9月7日土曜日
Auto Reference Counted wrapper
13年9月7日土曜日
extern mod extra;
use extra : : arc : :Arc;
let (port , chan) = stream( );
let pi = 3.14159;
let shared_num = Arc : : new(num);
13年9月7日土曜日
extern mod extra;
use extra : : arc : :Arc;
let (port , chan) = stream( );
let pi = 3.14159;
let shared_num = Arc : : new(num);
	

 do spawn {
	

 	

 let local_num :Arc<float> = port.recv();
	

 	

 println( fmt!( " % ? ", local_num . get( )));
	

 }
13年9月7日土曜日
http://saneyukis.hatenablog.com/entry/2013/07/28/233325
http://static.rust-lang.org/doc/tutorial-tasks.html
http://static.rust-lang.org/doc/rust.html
URL
13年9月7日土曜日
4.タスクの失敗
13年9月7日土曜日
“exceptions in Rust are
unrecoverable within a single task:
once a task fails, there is no way to
"catch" the exception.”
http://static.rust-lang.org/doc/tutorial-tasks.html
13年9月7日土曜日
タスクの例外は回復できない
13年9月7日土曜日
例外はfail!( )を呼ぶことで発生できる
13年9月7日土曜日
let result : Result<( ) , ( )> = do task : : try {
fail( );
};
assert!(result . is_err( ));
13年9月7日土曜日
基本的にタスクが死ぬと
関連するタスク全て死ぬ
13年9月7日土曜日
do spawn{
do spawn {
fail( );
}
hoge1( );
}
hoge2( );
13年9月7日土曜日

Rustのタスクモデルについて