getOpt with shared

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri May 11 18:18:35 UTC 2018


On Friday, May 11, 2018 18:01:18 Danny Arends via Digitalmars-d-learn wrote:
> On Friday, 11 May 2018 at 17:49:17 UTC, Jonathan M Davis wrote:
> > On Friday, May 11, 2018 17:25:44 Danny Arends via
> >
> > Digitalmars-d-learn wrote:
> >> [...]
> >
> > getopt is designed to be single-threaded. The keyword shared is
> > not used a single type in that module. If you want to use
> > shared with anything in D, you have three options:
> >
> > [...]
>
> Hey Jonathan,
>
> Thanks for the long and insightful answer.
> The object is indeed constructed from the main thread, but
> afterwards multiple threads need to read the values given via the
> command line. since everything in the object is read only I was
> hoping to get away with making it shared.
>
> I should just define tls variables to use with getopt and then
> set the corresponding variables in the shared object.

If you want to operate on that data as shared, then yes. But if you're
really just looking for each thread to have its own copy, I'd suggest that
you either give each thread its own copy on thread creation or pass it using
std.concurrency rather than trying to deal with a shared variable -
especially since the type system has no way to know that you're just
planning to read from the shared variable after that, and it will scream at
you for various operations - and once shared is fully locked down, it will
probably scream if you do much of _anything_ with shared, since at that
point, the compiler would only allow operations that were either marked with
shared or where it could guarantee that they were thread-safe.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list