Simple threads...
This commit is contained in:
parent
1052fa8aae
commit
135577fee9
|
@ -1,9 +1,8 @@
|
||||||
async void long_operation() {}
|
async void do_smth () {}
|
||||||
|
async void async_method(int idx, int max, int step, MainLoopKeeper mlk) {
|
||||||
async void async_method(int idx, int max, MainLoopKeeper mlk) {
|
|
||||||
for(var i = 0; i < max; ++i) {
|
for(var i = 0; i < max; ++i) {
|
||||||
stdout.puts(@"async$idx:$i\n");
|
stdout.puts(@"Async$idx:$i\n");
|
||||||
yield long_operation();
|
for(var j = 0; j < step; ++j) yield do_smth();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,12 +14,15 @@ public class MainLoopKeeper {
|
||||||
|
|
||||||
void start_methods(MainLoop loop) {
|
void start_methods(MainLoop loop) {
|
||||||
var mlk = new MainLoopKeeper(loop);
|
var mlk = new MainLoopKeeper(loop);
|
||||||
async_method.begin(1, 8, mlk);
|
async_method.begin(1, 8, 1, mlk);
|
||||||
async_method.begin(2, 3, mlk);
|
async_method.begin(2, 3, 2, mlk);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
var loop = new MainLoop();
|
var loop = new MainLoop();
|
||||||
|
stdout.puts("main:1\n");
|
||||||
start_methods(loop);
|
start_methods(loop);
|
||||||
|
stdout.puts("main:2\n");
|
||||||
loop.run();
|
loop.run();
|
||||||
|
stdout.puts("main:3\n");
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
class Test.Async : GLib.Object {
|
||||||
|
public async string say(string sentence) {
|
||||||
|
GLib.Idle.add(this.say.callback); // Useful in background threads
|
||||||
|
yield; // (yield called in the parent(main) thread).
|
||||||
|
return sentence;
|
||||||
|
}
|
||||||
|
public static int main(string[] args) {
|
||||||
|
Test.Async myasync = new Test.Async();
|
||||||
|
GLib.MainLoop mainloop = new GLib.MainLoop();
|
||||||
|
myasync.say.begin("helloworld",
|
||||||
|
(obj, res) => {
|
||||||
|
string sentence = myasync.say.end(res);
|
||||||
|
print("%s\n", sentence);
|
||||||
|
mainloop.quit();
|
||||||
|
});
|
||||||
|
mainloop.run();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
async double async_method() {
|
||||||
|
SourceFunc callback = async_method.callback;
|
||||||
|
|
||||||
|
double result = 0;
|
||||||
|
|
||||||
|
ThreadFunc<void*> run = () => {
|
||||||
|
stdout.puts("Thread is running!\n");
|
||||||
|
result = 3.14;
|
||||||
|
stdout.puts("Thread stopped!\n");
|
||||||
|
Idle.add((owned)callback);
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
new Thread<void*>("debugging thread name", run);
|
||||||
|
yield;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
void main() {
|
||||||
|
MainLoop mainloop = new GLib.MainLoop();
|
||||||
|
async_method.begin((obj,res) => {
|
||||||
|
var result = async_method.end(res);
|
||||||
|
stdout.puts(@"async_method.callback()\nResult = $result\n");
|
||||||
|
mainloop.quit();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
mainloop.run();
|
||||||
|
}
|
Loading…
Reference in New Issue