-
Notifications
You must be signed in to change notification settings - Fork 1
/
threads_attack.rb
65 lines (55 loc) · 1.14 KB
/
threads_attack.rb
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
require 'concurrent'
require 'thread'
NUM_ITERS = 1_000_000
# Data race
class MyMutex
def initialize
@thread1_wants_lock = Concurrent::AtomicBoolean.new(false)
@thread2_wants_lock = Concurrent::AtomicBoolean.new(false)
@turn = Concurrent::AtomicFixnum.new(0)
end
def lock(thread_idx)
# "Spinlock"
if thread_idx == 1
@thread1_wants_lock = true
# @thread1_wants_lock.make_true
@turn.value = 2
while @thread2_wants_lock && (@turn.value == 2)
# Busy loop
end
else
@thread2_wants_lock = true
# @thread2_wants_lock.make_true
@turn.value = 1
while @thread1_wants_lock && (@turn.value == 1)
# Busy loop
end
end
end
def unlock(thread_idx)
if thread_idx == 1
@thread1_wants_lock.make_false
else
@thread2_wants_lock.make_false
end
end
end
i = 0
lock_for_i = MyMutex.new
thr1 = Thread.new do
NUM_ITERS.times do
lock_for_i.lock(1)
i = i + 1
lock_for_i.unlock(1)
end
end
thr2 = Thread.new do
NUM_ITERS.times do
lock_for_i.lock(2)
i = i + 1
lock_for_i.unlock(2)
end
end
thr1.join
thr2.join
puts i