Skip to content

Commit

Permalink
Add mutex challenges
Browse files Browse the repository at this point in the history
  • Loading branch information
rusinikita authored and Nikita Rusin committed Sep 17, 2023
1 parent 1afea16 commit 55fee62
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ go install github.com/rusinikita/trainer@latest
```
trainer
```

### Thanks

Some questions inspired by [this quiz](https://github.com/smallnest/go-concurrent-quiz)
73 changes: 73 additions & 0 deletions challenge/files/mutex.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name = "Mutex"
category = "concurrency"
default_code_snippet = """
var mu sync.Mutex
var chain string
func main() {
chain = "main"
A()
fmt.Println(chain)
}
func A() {
mu.Lock()
defer mu.Unlock()
chain = chain + " --> A"
B()
}
func B() {
chain = chain + " --> B"
C()
}
func C() {
mu.Lock()
defer mu.Unlock()
chain = chain + " --> C"
}
"""

[[questions]]
text = "Select program output"
type = "select_answers"
answers = [
{ text = "can't compile" },
{ text = "main --> A --> B --> C" },
{ text = "main" },
{ text = "deadlock!", code_line_ranges = [] },
]

[[questions]]
text = "Select line causes deadlock"
type = "select_answers"
answers = [
{ text = "Program stops here", code_line_ranges = [[22]] },
]

[[questions]]
text = "How to change lines 11-13 to fix deadlock? Two answers"
type = "select_answers"
answers = [
{ text = '''
defer mu.Unlock()
chain = chain + " --> A"
B()
''' },
{ text = '''
chain = chain + " --> A"
defer mu.Unlock()
B()
''' },
{ text = '''
chain = chain + " --> A"
mu.Unlock()
B()
''', code_line_ranges = [] },
{ text = '''
defer B()
defer mu.Unlock()
chain = chain + " --> A"
''', code_line_ranges = [] },
]
58 changes: 58 additions & 0 deletions challenge/files/rwmutex.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name = "RWMutex"
category = "concurrency"
default_code_snippet = """
var mu sync.RWMutex
var count int
func main() {
go A()
time.Sleep(time.Millisecond)
mu.Lock()
defer mu.Unlock()
count++
fmt.Println(count)
}
func A() {
mu.RLock()
defer mu.RUnlock()
B()
}
func B() {
time.Sleep(5 * time.Second)
C()
}
func C() {
mu.RLock()
defer mu.RUnlock()
}
"""

[[questions]]
text = "Select program output"
type = "select_answers"
answers = [
{ text = "can't compile" },
{ text = "1" },
{ text = "no answer" },
{ text = "deadlock!", code_line_ranges = [] },
]

[[questions]]
text = "Select line causes deadlock."
type = "select_answers"
answers = [
{ text = "Program stops here", code_line_ranges = [[6]] },
]

[[questions]]
text = "One deleted line can fix program. Which one?"
type = "select_answers"
answers = [
{ text = 'This line in main()', code_line_ranges = [[5]] },
{ text = 'This line in A()' },
{ text = 'This line in B()', code_line_ranges = [[19]] },
{ text = 'This line in C()' },
]

0 comments on commit 55fee62

Please sign in to comment.