It is not ok to read some memory location (grabbing the data) while another
goroutine is writing to the same memory location (editing the entities).
Also, it is not ok if two goroutines simultaneously write to the same
These are called "data races" and are bad, because, for example, the result
of reading some memory while another thread is modifying it
The race detector is a tool that detects this kind of bugs. You turn it on
by doing go run, build or test with the -race flag.
By the way, it is ok if multiple goroutines read from the same memory
location, if no one is modifying the data.
When using multiple goroutines you typically have two options:
1) using shared data structures (your Entity map) and synchronize access
2) avoid shared data structures and have the goroutines communicate with
Of course you can mix those things.
Note that these are issues that arise in any language with multithreaded
programming, not just Go.
Go has been designed with concurrency in mind and has better tools than,
for example, Java or C,
but concurrent programming is not trivial and you should know what you are
Look for tutorials on the matter.
In your case, it would be ok, for example, if you could split your set of
entities in multiple subsets and
assign each subset to a different goroutine. The goroutines would work in
parallel, but each one would mind it's own business
(not mess with data belonging to the others).
On Saturday, April 2, 2016 at 10:29:24 PM UTC+2, zachary...@gmail.com wrote:
So what if one goroutine is editing the entities and one is just grabbing
the data (to send to other players), would that be okay?
And what is the race detector?
On Saturday, April 2, 2016 at 1:20:59 PM UTC-7, Roberto Zanotto wrote:
Just make sure that two different goroutines don't edit the same Entity
at the same time.
Run your code with "go run -race whatever.go" to enable the race detector.
On Saturday, April 2, 2016 at 10:11:14 PM UTC+2, zachary...@gmail.com
I'm making a server for a game, and I currently have an array of
entities created on the main thread, looks like this
var entities = make(map[string]*Entity)
So my issue now is that I have to loop through every entity and call a
function called entity.update() which will do stuff like update their
health, energy, and position values. I'm also having to call a couple other
update functions which gets pretty laggy. Could I put these functions on
another goroutine? They aren't adding or removing elements from the array,
just editing the elements' values.
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to email@example.com.
For more options, visit https://groups.google.com/d/optout.