morphologica icon indicating copy to clipboard operation
morphologica copied to clipboard

Consider thread safety of Vector (and vVector) classes

Open sebjameswml opened this issue 2 years ago • 3 comments

@niclar writes: "The underlying data structure of the Vector class is a std::array which if misaligned is subject to write tearing. (Further the atomic read/write assumption (guaranteed on x86/x64 for the fundamental datatypes, int, double..) should probably be asserted if the Vector was to be used in a multi threaded context)"

sebjameswml avatar Mar 16 '22 10:03 sebjameswml

-One might argue that since you leave all threading up to the user it's their responsibility to ensure the above.

niclar avatar Mar 16 '22 12:03 niclar

That's true, but I would at least like to have some comments in the code declaring exactly how thread safe/unsafe the class is. From this: https://docs.microsoft.com/en-us/cpp/standard-library/thread-safety-in-the-cpp-standard-library?view=msvc-170 I think that it's safe to read from a single Vector or vVector instance in different threads, but not to write to them from separate threads without ensuring that the writes occur at different times.

sebjameswml avatar Mar 16 '22 13:03 sebjameswml

Useful info on std::atomic: https://stackoverflow.com/questions/31978324/what-exactly-is-stdatomic

sebjameswml avatar May 06 '22 09:05 sebjameswml

These are as thread safe as std::array or std::vector. Closing.

sebjameswml avatar Feb 01 '23 14:02 sebjameswml