-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1afea16
commit 55fee62
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] }, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()' }, | ||
] |