diff options
author | MaurĂcio Antunes <mauricio.abreua@gmail.com> | 2024-02-14 13:47:56 -0300 |
---|---|---|
committer | MaurĂcio Antunes <mauricio.abreua@gmail.com> | 2024-02-14 13:47:56 -0300 |
commit | 3eaddc8b3528c41a7f51dd8f7738344a56ae451e (patch) | |
tree | 3e519bc347e5035d2de1ceba535a32064d5db3ec /exercises/concurrent/concurrent2 | |
parent | 740ad3633665e74ca1df54085b6de940c4b67432 (diff) |
feat: New exercises (concurrency)
Diffstat (limited to 'exercises/concurrent/concurrent2')
-rw-r--r-- | exercises/concurrent/concurrent2/main_test.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/exercises/concurrent/concurrent2/main_test.go b/exercises/concurrent/concurrent2/main_test.go new file mode 100644 index 0000000..2233259 --- /dev/null +++ b/exercises/concurrent/concurrent2/main_test.go @@ -0,0 +1,32 @@ +// concurrent2 +// Make the tests pass! + +// I AM NOT DONE +package main_test + +import ( + "sync" + "testing" +) + +func TestCounter(t *testing.T) { + counter := updateCounter() + if counter != 100 { + t.Errorf("Counter should be 100, but got %d", counter) + } +} + +func updateCounter() int { + var counter int + var wg sync.WaitGroup + + for i := 0; i < 100; i++ { + wg.Add(1) + go func() { + defer wg.Done() + counter++ // Many goroutines trying to update the counter? We need some protection here! + }() + } + wg.Wait() + return counter +} |