This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMain.hx
69 lines (55 loc) · 1.39 KB
/
Main.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package;
// http://old.haxe.org/doc/neko/threads
// http://api.haxe.org/cpp/vm/Thread.html
// http://www.joshuagranick.com/2012/08/24/using-threads-with-nme/
// http://api.haxe.org/cpp/vm/Deque.html
#if cpp
import cpp.vm.Mutex;
import cpp.vm.Thread;
#elseif neko
import neko.vm.Mutex;
import neko.vm.Thread;
#end
class ShareData {
public var value:Int;
public var Value (get, set):Int;
private var mutex:Mutex;
public function new () {
mutex = new Mutex ();
}
private function get_Value ():Int {
mutex.acquire ();
var result = value;
mutex.release ();
return result;
}
private function set_Value (newValue:Int):Int {
mutex.acquire ();
value = newValue;
mutex.release ();
return newValue;
}
}
class Main {
private static function setValue ():Void {
var data = Thread.readMessage (true);
data.value = 100;
}
private static function retrieveValue ():Void {
var data = Thread.readMessage (true);
trace ("The value is " + data.value);
}
public static function main () {
//----------------------------------------------------------------------
trace('--- Sharing an object between threads ---');
var data = new ShareData ();
var first = Thread.create (setValue);
var second = Thread.create (retrieveValue);
first.sendMessage (data);
second.sendMessage (data);
for (i in 0...10) {
trace ("Main Thread: " + i);
Sys.sleep (0.5);
}
}
}