multithreading - How to define thread safe array? -
how can define thread safe global array minimal modifications?
i want every access accomplished using mutex , synchronized block.
something 't' type (note 'sync' keyword not defined afaik):
sync array!(t) syncvar;
and every access simmilar this:
mutex __syncvar_mutex; //some func scope.... synchronized(__syncvar_mutex) { /* edits 'syncvar' safely */ }
my naive attempt this:
import std.typecons : proxy: synchronized class array(t) { static import std.array; private std.array.array!t data; mixin proxy!data; }
sadly, doesn't work because of https://issues.dlang.org/show_bug.cgi?id=14509
can't surprised though automagical handling of multi-threading via hidden mutexes unidiomatic in modern d , concept of synchronized classes relict d1 times.
you can implement same solution manually, of course, defining own sharedarray
class necessary methods , adding locks inside methods before calling internal private plain array
methods. presume want work more out of box.
can't invent better right here , (will think more) worth noting in general encouraged in d create data structures designed handling shared access explicitly instead of protecting normal data structures mutexes. and, of course, encouraged approach not shared data @ using message passing instead.
i update answer if better comes mind.
Comments
Post a Comment