Skip to content

Commit 8f05ef6

Browse files
author
Markus Ho-Yen
committed
added parts for go and python
1 parent 04918c7 commit 8f05ef6

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Part4/go/foo.go

+8
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@ var i = 0
1212

1313
func incrementing() {
1414
//TODO: increment i 1000000 times
15+
for i_inc := 0; i_inc < 1000000; i_inc++ {
16+
i++
17+
}
1518
}
1619

1720
func decrementing() {
1821
//TODO: decrement i 1000000 times
22+
for i_dec := 0; i_dec < 1000000; i_dec++ {
23+
i--
24+
}
1925
}
2026

2127
func main() {
2228
runtime.GOMAXPROCS(runtime.NumCPU()) // I guess this is a hint to what GOMAXPROCS does...
2329
// Try doing the exercise both with and without it!
2430

2531
// TODO: Spawn both functions as goroutines
32+
go incrementing()
33+
go decrementing()
2634

2735
// We have no way to wait for the completion of a goroutine (without additional syncronization of some sort)
2836
// We'll come back to using channels in Exercise 2. For now: Sleep.

Part4/python/foo.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,27 @@
1212
def incrementingFunction():
1313
global i
1414
# TODO: increment i 1_000_000 times
15+
for count in range(1000000):
16+
i += 1
1517

1618
def decrementingFunction():
1719
global i
1820
# TODO: decrement i 1_000_000 times
19-
21+
for count in range(1000000):
22+
i -= 1
2023

2124

2225
def main():
2326
# TODO: Something is missing here (needed to print i)
27+
global i
2428

2529
incrementing = Thread(target = incrementingFunction, args = (),)
2630
decrementing = Thread(target = decrementingFunction, args = (),)
2731

2832
# TODO: Start both threads
29-
33+
incrementing.start()
34+
decrementing.start()
35+
3036
incrementing.join()
3137
decrementing.join()
3238

0 commit comments

Comments
 (0)