phobos / tango / ares
Sean Kelly
sean at f4.ca
Sat Feb 10 23:51:43 PST 2007
kris wrote:
> Charles D Hixson wrote:
>> I'm not sure whether you were quoting the documentation, or reporting
>> your understanding. If you were quoting the documentation, I think it
>> needs editing.
>
> From the doc: "Please note that the class itself is stateful, and
> therefore a single instance is not shareable across multiple threads."
>
> Thus, it is considered unwise to share a single instance of Sprint
> across multiple threads. However, multiple threads /can/ share a single
> instance if they follow this pattern:
>
> synchronized (GlobalSprint)
> GlobalSprint ("do my formatting", with, these, args);
>
> This is a fairly standard mechanism in D for sharing resources across
> threads, and it's what I had referred to.
For what it's worth, Tango also provides thread-local storage, and while
it isn't ideal from a semantic standpoint (a bona-fide 'local' storage
class would be much nicer), it's an option worth considering. It could
be used like so:
auto TLSPrint = new ThreadLocal!(Sprint);
// do this in each thread
TLSPrint.val = new Sprint;
// use the local Sprint instance
TLSPrint.val()( "do my formatting", with, these, args );
Accessing a TLS value tyically amounts to a few pointer dereferences so
it's far less costly than acquiring a mutex, which may or may not be
important to you.
As a side-note... Tango currently provides 64 TLS "slots". This amount
is fixed mostly to keep performance as high as possible, but it could
easily be increased if it turns out not to be enough.
Sean
More information about the Digitalmars-d-learn
mailing list