Simple threads...

This commit is contained in:
Kolan Sh 2015-08-19 14:28:12 +03:00
parent 0361cfc4d9
commit ca6976bda1
2 changed files with 37 additions and 1 deletions

View File

@ -1,4 +1,4 @@
public class MyThread {
public class MyThread : Object {
private string _s;
@ -8,7 +8,9 @@ public class MyThread {
public int run () {
stdout.printf ("%s\n", _s);
//Thread.usleep(3000000);
return 0;
//Thread.exit (0); // the same
}
}

34
vala/threads/lock.vala Normal file
View File

@ -0,0 +1,34 @@
public class MyThread : Object {
public int lock_var = 0;
public int increase_lock_var () {
var ret = 0;
lock (lock_var) ret = ++lock_var;
return ret;
}
public int run () {
for (var i = 0; i < 8; ++i) {
var tmp = 0;
lock (lock_var) tmp = lock_var++;
stdout.puts (@"thrd:$lock_var\n");
Thread.usleep(100);
}
return 0;
}
}
void main () {
var mt = new MyThread ();
var t = new Thread<int> ("Debugging thread name", mt.run);
for (var i = 0; i < 8; ++i) {
var tmp = mt.increase_lock_var ();
stdout.puts(@"main:$tmp\n");
Thread.usleep(100);
}
t.join ();
}