Add concurrency

This commit is contained in:
dwrz
2026-10-27 12:41:06 +00:00
commit 2334f4eaf0
31 changed files with 43061 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
package main
import (
"fmt"
"sync"
)
func main() {
var balance int = 100
// var mu sync.Mutex
fmt.Println("Initial balance:", balance)
// Apply concurrent adjustments to the balance.
// The sum of these adjustments is 0.
// We should see no changes in balance at the end.
var adjustments = []int{
-8, -4, -2, 0, 2, 4, 8,
-8, -4, -2, 0, 2, 4, 8,
-8, -4, -2, 0, 2, 4, 8,
-8, -4, -2, 0, 2, 4, 8,
-8, -4, -2, 0, 2, 4, 8,
-8, -4, -2, 0, 2, 4, 8,
}
var wg sync.WaitGroup
wg.Add(len(adjustments))
for _, adjustment := range adjustments {
go func(adjustment int) {
defer wg.Done()
// mu.Lock()
balance += adjustment
// mu.Unlock()
}(adjustment)
}
wg.Wait()
fmt.Println("Final balance:", balance)
}
+7
View File
@@ -0,0 +1,7 @@
#!/bin/bash
for (( ; ; )) do
go run ./main.go
printf "\n"
sleep 0.5
done