dev/vala/threads/async_ex.vala

31 lines
676 B
Vala
Raw Normal View History

2015-08-20 21:09:58 +03:00
async void async_method(int idx, int max, int step, MainLoopKeeper mlk) {
2015-08-20 18:28:02 +03:00
for(var i = 0; i < max; ++i) {
2015-08-20 21:09:58 +03:00
stdout.puts(@"Async$idx:$i\n");
2018-08-20 19:10:53 +03:00
for(var j = 0; j < step; ++j) {
Idle.add(async_method.callback);
yield;
}
2015-08-20 18:28:02 +03:00
}
}
public class MainLoopKeeper {
MainLoop _loop;
public MainLoopKeeper(MainLoop loop) { _loop = loop; }
~MainLoopKeeper() { _loop.quit(); }
}
void start_methods(MainLoop loop) {
var mlk = new MainLoopKeeper(loop);
2015-08-20 21:09:58 +03:00
async_method.begin(1, 8, 1, mlk);
async_method.begin(2, 3, 2, mlk);
2015-08-20 18:28:02 +03:00
}
void main() {
var loop = new MainLoop();
2015-08-20 21:09:58 +03:00
stdout.puts("main:1\n");
2015-08-20 18:28:02 +03:00
start_methods(loop);
2015-08-20 21:09:58 +03:00
stdout.puts("main:2\n");
2015-08-20 18:28:02 +03:00
loop.run();
2015-08-20 21:09:58 +03:00
stdout.puts("main:3\n");
2015-08-20 18:28:02 +03:00
}