-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A code for SAFE use across concurrent goroutines? #78
Comments
Your code has two data races:
Here's a version that achieves both of those with a single mutex. In this specific example this makes concurrency unnecessary since it essentially makes all the code in the worker sequential, but in a real world example where the worker code does more than this synchronized section, it would be potentially different. |
Thanks. I was fixing that code because it was a mistake for the Using mu.Lock()
for i := 0; i < cap(ids)/workers; i++ {
ids = append(ids, ulid.MustNew(ulid.Timestamp(time.Now()), entropy))
}
mu.Unlock() But my question was about using ulid with common libraries such ent, gqlgen and using functions like: func NewID() string {
return ulid.MustNew(ulid.Timestamp(time.Now()), entropy).String()
} Is there a safe way to use the package? What I'm looking for is an example for the code to be used for creating IDs used by common libraries that use various goroutines internally. With my example I tried to replicate that functioning. Made myself clear? |
Superseded by #79. |
I'm trying to use for the first time the ulid package, but using the below code I'm getting panics everywhere.
Can you help me understand why and how to write code SAFE for concurrent use across goroutines?
REPL: https://go.dev/play/p/Ysr8cCgF44n
The text was updated successfully, but these errors were encountered: