Are primitive variable values like int, bool etc. atomic in GDScript? #4983
Replies: 7 comments 5 replies
-
As far as I know they are not guaranteed to be atomic. Also, that would be a Variant that happens to have a bool value. I'd like to atomic integers in Godot… But, I had not run into a situation where having them was critical. |
Beta Was this translation helpful? Give feedback.
-
GDScript arrays, dictionaries In GDScript, reading and writing elements from multiple threads is OK, but anything that changes the container size (resizing, adding or removing elements) requires locking a mutex. So if a read or write in an array or dictionary is thread-safe then a simple read or write on primitive values should do too. Right?! |
Beta Was this translation helpful? Give feedback.
-
Agreed. That was my next step to find out if int-variable += 1 is atomic too.
The documentation is too little precise on this.
On Aug 4, 2022, 04:00, at 04:00, "Alfonso J. Ramos" ***@***.***> wrote:
Beyond if the read and write of an int is atomic. I would want atomic
increments and decrements, exchange and compare exchange. Otherwise
even with atomic read and write we still have the [ABA
problem](https://en.wikipedia.org/wiki/ABA_problem).
--
Reply to this email directly or view it on GitHub:
#4983 (reply in thread)
You are receiving this because you authored the thread.
Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Thank you for your analysis.
The reason why I am asking this, to avoid synchronization when it's unnecessary on a language level. 👍
On Aug 2, 2022, 17:36, at 17:36, "Alfonso J. Ramos" ***@***.***> wrote:
Writing the `Array`
(https://github.com/godotengine/godot/blob/3.x/core/array.cpp#L74) uses
`write` from the member `array` of the member `_p` of `Array`.
The member `_p` of `Array` is declared as `PrivateArray`
(https://github.com/godotengine/godot/blob/3.x/core/array.h#L42).
The `array` member of `PrivateArray` is a `Vector<Variant>`
(https://github.com/godotengine/godot/blob/3.x/core/array.cpp#L41).
`Vector<Variant>.write` is a `VectorWriteProxy<Variant>`?
(https://github.com/godotengine/godot/blob/3.x/core/vector.h#L60), oh
it is a function that just writes
(https://github.com/godotengine/godot/blob/3.x/core/vector.h#L51) (with
a bound check and marked to be inlined).
And `Variant` - by the way - is like a discriminated union
(https://github.com/godotengine/godot/blob/3.x/core/variant.h#L135-L166).
Perhaps they mean by "thread-safe" that it won't crash? Anyway, I don't
see how that would be an atomic write. *I might be missing something,
as there are things I don't understand, but I as far as I can tell
`Variant` would be too large to be automatically atomic, and
`Vector<Variant>` does not seem to do something to help it either.*
--
Reply to this email directly or view it on GitHub:
#4983 (reply in thread)
You are receiving this because you authored the thread.
Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I found following post: |
Beta Was this translation helpful? Give feedback.
-
To sum it up: If there are lots of readers and one writer only then we do not need synchronization. If there are more than one writer, synchronization of shared resources is necessary to always have the most up-to-date version I think what the chapter about thread-safety in the context of collections means is that a reader gets accurate data - it might just be out-dated due to COW which is the mechanism to ensure that all data is accurate at all. |
Beta Was this translation helpful? Give feedback.
-
I've been pointing out how threaded access can go wrong. For a change, let me tell you a pattern that will work:
Notice that at you only size the array at the start, you never resize it. And also notice that at no point will two threads try to access the same value on the array. |
Beta Was this translation helpful? Give feedback.
-
I've read the chapter about threading in Godot.
Multiple Threads
At the end of the chapter is an example given about Semaphores.
Semaphores
In method _exit_tree() a boolean variable named exit_thread is encapsulated by a mutex.
It implies that boolean variables are NOT atomic on read or write access.
I was assuming primitive values are always atomic even in a multi-threaded context.
A boolean is either true or false.
So what's the truth behind it?
Beta Was this translation helpful? Give feedback.
All reactions