EngoEngine / engo

Engo is an open-source 2D game engine written in Go.
https://engoengine.github.io
MIT License
1.74k stars 136 forks source link

Concurrent map read and map write #685

Open inkeliz opened 4 years ago

inkeliz commented 4 years ago

Golang is a language that have a good support for concurrency and multithreading. I'm testing some stuff, as always, but I'm hit at fatal error: concurrent map read and map write.

Seems that Engo isn't capable to support multi-thread by default. There's a lot of map without any mutex:

https://github.com/EngoEngine/engo/blob/d2790d2de842bc8298ace6abb7a1136ee0a9aba5/common/render.go#L277-L281 https://github.com/EngoEngine/engo/blob/d2790d2de842bc8298ace6abb7a1136ee0a9aba5/common/spritesheet.go#L113-L127

That error can be triggered with anything like:

for i := 0; i < 10; i++ {
    go func() {
        //...
        element.RenderSystem.Add(
    }()
}

...


I'm thinking about create a queued channel, intended to receive and then calls Engo.

There's any plan to support concurrency?

Noofbiz commented 4 years ago

I think supporting concurrency in systems would be an amazing thing! There are a few issues that would need to be worked through, mostly that openGL calls MUST be done on the main thread, the operating system requires it. So the render system or any system that does drawing would have to be guaranteed somehow to run on the main thread.