diff options
| author | faiface <faiface2202@gmail.com> | 2019-05-06 19:11:18 +0200 |
|---|---|---|
| committer | faiface <faiface2202@gmail.com> | 2019-05-06 19:11:18 +0200 |
| commit | f5440dd2956919caf89701dd226e31fb8bc035f5 (patch) | |
| tree | baef6accc9b1b8b03ebb16cedcc8b5f8114d47b3 | |
| parent | ebb42b7777c988f802d863251512225e53b92b8f (diff) | |
| download | gui-f5440dd2956919caf89701dd226e31fb8bc035f5.zip | |
README: add 'A note on race conditions'
| -rw-r--r-- | README.md | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -309,6 +309,40 @@ They won't, because the channels of events have unlimited capacity and never blo And that's basically all you need to know about `faiface/gui`! Happy hacking! +## A note on race conditions + +There is no guarantee when a function sent to the `Draw()` channel will be executed, or if at all. Look at this code: + +```go +pressed := false + +env.Draw() <- func(drw draw.Image) image.Rectangle { + // use pressed somehow +} + +// change pressed somewhere here +``` + +The code above has a danger of a race condition. The code that changes the `pressed` variable and the code that uses it may run concurrently. + +**My advice is to never enclose an outer variable in a drawing function.** + +Instead, you can do this: + +```go +redraw := func(pressed bool) func(draw.Image) image.Rectangle { + return func(drw draw.Image) image.Rectangle { + // use the pressed argument + } +} + +pressed := false + +env.Draw() <- redraw(pressed) + +// changing pressed here doesn't cause race conditions +``` + ## Licence [MIT](LICENCE) |