Using goroutine with sync.WaitGroup results in inconsistent results
In the Go language, goroutine can be used to execute tasks concurrently, and sync.WaitGroup is a synchronization mechanism used to wait for the completion of a group of goroutines. However, PHP Editor Banana found that in some cases, using goroutine with sync.WaitGroup may lead to inconsistent results. This problem usually occurs when multiple goroutines modify shared variables at the same time. Since the execution order of goroutines is uncertain, it may lead to inconsistency in the final results. In this article, we will explore the causes of this problem and provide some solutions to ensure result consistency between goroutines.
Question content
I am trying to use goroutine (in Go lang) to count the number of prime numbers less than an arbitrary integer i
.
For example, if i
is 100, the result should be 25
.
The following is my current implementation:
<code>package "main" import ( "fmt" "math" "sync" "time" ) var wg sync.WaitGroup func isprime(x int) bool { if x == 2 { return true } if x == 1 || x%2 == 0 { return false } var xi = float64(x) for i := 3; float64(i) < (math.Pow(xi, 0.5) + 1.0); i += 2.0 { if x%i == 0 { return false } } return true } func main() { fmt.Print("Till what number should I count primes? ") var i int fmt.Scan(&i) r := 0 pr := &r fmt.Println("Counting primes till ", i) start := time.Now() for x := 0; x <= i; x++ { wg.Add(1) go func(n int) { defer wg.Done() if isprime(n) { *pr += 1 } }(x) } wg.Wait() elapsed := time.Since(start).Seconds() fmt.Println("Counted", r, "primes") fmt.Println("took", elapsed, "seconds") } </code>
When I run this program, I get correct results for smaller i
values (up to about 1000)
But for larger i
values, the results are inconsistent and incorrect.
❯ ./main Till what number should I count primes? 10000 Counting primes till 10000 Counted 1228 primes took 0.006776541 seconds ❯ ./main Till what number should I count primes? 10000 Counting primes till 10000 Counted 1227 primes took 0.004183875 seconds ❯ ./main Till what number should I count primes? 1000000 Counting primes till 1000000 Counted 78254 primes took 0.441985921 seconds ❯ ./main Till what number should I count primes? 1000000 Counting primes till 1000000 Counted 78327 primes took 0.430042047 seconds
As the value of i
becomes larger, the result fluctuation increases. What causes this? Is there any way to make it consistent and correct?
Workaround
You have a shared variable without proper synchronization. A race condition exists (*pr = 1
). Adding mutexes before and after the shared variable fixes it (mu.Lock(), mu.Unlock()
).
Code:
var wg sync.WaitGroup var mu sync.Mutex func main() { fmt.Print("Till what number should I count primes? ") var i int fmt.Scan(&i) r := 0 pr := &r fmt.Println("Counting primes till ", i) start := time.Now() for x := 0; x <= i; x++ { wg.Add(1) go func(n int) { defer wg.Done() if isprime(n) { mu.Lock() // <= lock *pr += 1 mu.Unlock() // <= unlock } }(x) } wg.Wait() elapsed := time.Since(start).Seconds() fmt.Println("Counted", r, "primes") fmt.Println("took", elapsed, "seconds") }
Output:
Till what number should I count primes? 1000000 Counting primes till 1000000 Counted 78498 primes took 0.6783484 seconds Till what number should I count primes? 1000000 Counting primes till 1000000 Counted 78498 primes took 0.5428273 seconds Till what number should I count primes? 1000000 Counting primes till 1000000 Counted 78498 primes took 0.5521617 seconds
The above is the detailed content of Using goroutine with sync.WaitGroup results in inconsistent results. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Multithreading is an important technology in computer programming and is used to improve program execution efficiency. In the C language, there are many ways to implement multithreading, including thread libraries, POSIX threads, and Windows API.

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

C language multithreading programming guide: Creating threads: Use the pthread_create() function to specify thread ID, properties, and thread functions. Thread synchronization: Prevent data competition through mutexes, semaphores, and conditional variables. Practical case: Use multi-threading to calculate the Fibonacci number, assign tasks to multiple threads and synchronize the results. Troubleshooting: Solve problems such as program crashes, thread stop responses, and performance bottlenecks.
