-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy paththreading.oc
60 lines (48 loc) · 1.13 KB
/
threading.oc
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
import std::thread::{ this, Thread, Mutex }
let count: i32 = 0
let mutex: Mutex
def start_routine(a: untyped_ptr): untyped_ptr {
for let i = 0; i < 100000; i++ {
mutex.lock()
count+=1
mutex.unlock()
}
return null
}
def second_routine(a: untyped_ptr): untyped_ptr {
for let i = 0; i < 100000; i++ {
mutex.lock()
count += 1
mutex.unlock()
}
return null
}
def test1(a: untyped_ptr): untyped_ptr {
for let i = 0; i < 3; i++ {
println("Hello from thread 1")
}
return null
}
def test2(a: untyped_ptr): untyped_ptr {
for let i = 0; i < 5; i++ {
println("Hello from thread 2")
}
return null
}
def main() {
let a = Thread::make(test1)
let b = Thread::make(test2)
a.set_detach_state()
b.set_detach_state()
defer a.destroy_attr()
defer b.destroy_attr()
a.start()
b.start()
thread::sleep(1)
}
def old_main() {
mutex = Mutex::make() // here by default is NORMAL but can provide RECURSIVE and ERRCHECK
let a = Thread::make(start_routine)
let b = Thread::make(second_routine)
println("%d\n",count)
}