Is there any tutorial on how to use the shared attribute on structs

IchorDev zxinsworld at gmail.com
Mon Aug 25 10:50:18 UTC 2025


On Sunday, 17 August 2025 at 21:00:32 UTC, solidstate1991 wrote:
> I created a nogc array handler struct, and I would like to use 
> it as a shared array.
>
> I can only easily find anything related to it for simple 
> built-in types, let alone for slices. If at least I could use 
> slices directly, it would be a great help (lock write access in 
> a `synchronized` scope, then only read from the slice).

Your question is not very clear.

You cannot find anything related to what? To using `shared`?

You also didn't clarify what you mean by 'use it as a shared 
array'. Do you mean you want to make an instance of your struct 
`shared`? You can do that by casting it to `shared` and away from 
`shared` (as long as you manually ensure thread safety with 
mutexes, or simply pass it to a different thread and discard the 
original):
```d
struct S{}

shared S s;

void main(){
	S sLocal;
	s = cast(shared)sLocal;
}
```

If you're asking how you can make your implementation `shared`, 
that's much more complicated and I wouldn't recommend it unless 
you know what you're getting into. `shared` functions are rarely 
implemented outside of things like synchronisation primitives 
(e.g. mutexes).


More information about the Digitalmars-d-learn mailing list