On Thursday, 21 July 2022 at 17:26:07 UTC, frame wrote:
The module creators want you to prevent from non-threadsafe actions. Merging into one variable can be such a non-threadsafe action, but it has not to be.

For example, if you have an array of fixed length or an already allocated one and each worker thread uses a fixed index to write into this result array variable then this operation may be threadsafe because each thread writes in different positions in the memory and the variable itself doesn't change.

If you are depending to overwrite an value instead, the operation is not considered threadsafe. For example just adding 1 + 2. Adding a value to an associative array is also not thread safe (it may re-allocate or re-index it's data)

We have basic tools to achieve this task. One is to lock actions with a synchronized block so each thread need to wait the other thread to complete before it can continue to execute the particular code position - and the other one are atomic operations that provide a better performance for simple operations because they are lock-free.

Why it's needed to know this? Because you may run into situations where have to deal with it, even the compiler keeps silent about.

https://dlang.org/spec/statement.html#synchronized-statement

I'm considering thread safety.

Looking for your task you might want to use a `WorkerLocalStorage` solution instead, look at the example:
https://dlang.org/phobos/std_parallelism.html#.TaskPool.WorkerLocalStorage

This looks interesting, thanks, I'll give it a try.


Reply via email to