Debian Codegraph @

test cases/vala/5 target glib/GLib.Thread.vala

raw
1 extern void * get_ret_code ();
2
3 public class MyThread : Object {
4 public int x_times { get; private set; }
5
6 public MyThread (int times) {
7 this.x_times = times;
8 }
9
10 public int run () {
11 for (int i = 0; i < this.x_times; i++) {
12 stdout.printf ("ping! %d/%d\n", i + 1, this.x_times);
13 Thread.usleep (10000);
14 }
15
16 // return & exit have the same effect
17 Thread.exit (get_ret_code ());
18 return 43;
19 }
20 }
21
22 public static int main (string[] args) {
23 // Check whether threads are supported:
24 if (Thread.supported () == false) {
25 stderr.printf ("Threads are not supported!\n");
26 return -1;
27 }
28
29 try {
30 // Start a thread:
31 MyThread my_thread = new MyThread (10);
32 Thread<int> thread = new Thread<int>.try ("My fst. thread", my_thread.run);
33
34 // Wait until thread finishes:
35 int result = thread.join ();
36 // Output: `Thread stopped! Return value: 42`
37 stdout.printf ("Thread stopped! Return value: %d\n", result);
38 } catch (Error e) {
39 stdout.printf ("Error: %s\n", e.message);
40 }
41
42 return 0;
43 }