diff --git a/vala/live.gnome.org_tutorial/run.sh b/vala/live.gnome.org_tutorial/run.sh new file mode 100755 index 0000000..29e8704 --- /dev/null +++ b/vala/live.gnome.org_tutorial/run.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +valac --thread --pkg glib-2.0 --target-glib=2.32 threading-sample.vala +./threading-sample diff --git a/vala/live.gnome.org_tutorial/threading-sample.vala b/vala/live.gnome.org_tutorial/threading-sample.vala new file mode 100644 index 0000000..c1fbfb0 --- /dev/null +++ b/vala/live.gnome.org_tutorial/threading-sample.vala @@ -0,0 +1,55 @@ +public class MyThread : Object { + public ThreadPriority priority { get; private set; } + public int x_times { get; private set; } + + public MyThread (int times, ThreadPriority priority) { + this.priority = priority; + this.x_times = times; + } + + public int run () { + // set the priority: + Thread.self ().set_priority (ThreadPriority.URGENT); + + for (int i = 0; i < this.x_times; i++) { + stdout.printf ("ping! %d/%d\n", i + 1, this.x_times); + Thread.usleep (10000); + } + + // return & exit have the same effect + Thread.exit (42); + return 43; + } +} + +public static int main (string[] args) { + // Check whether threads are supported: + if (Thread.supported () == false) { + stderr.printf ("Threads are not supported!\n"); + return -1; + } + + try { + // Start a thread: + MyThread my_thread = new MyThread (10, ThreadPriority.URGENT); + Thread thread = new Thread.try ("My fst. thread", my_thread.run); + + // Count all running threads: + int threads = 0; + Thread.foreach (() => { + threads++; + }); + + // Output: ``Running threads: 0`` + stdout.printf ("Running threads: %d\n", threads); + + // Wait until thread finishes: + int result = thread.join (); + // Output: `Thread stopped! Return value: 42` + stdout.printf ("Thread stopped! Return value: %d\n", result); + } catch (Error e) { + stdout.printf ("Error: %s\n", e.message); + } + + return 0; +}