Modify thread-local storage from parent thread

Steven Schveighoffer schveiguy at yahoo.com
Mon Aug 8 12:38:34 PDT 2011


On Mon, 08 Aug 2011 14:17:28 -0400, Kai Meyer <kai at unixlords.com> wrote:

> I am playing with threading, and I am doing something like this:
>          file.rawRead(bytes);
>          auto tmpTask = task!do_something(bytes.idup);
>          task_pool.put(tmpTask);
> Is there a way to avoid the idup (or can somebody explain why idup here  
> is not expensive?)

I'd have to see where bytes is created, if it's created in the same  
context, just casting to immutable is allowed, as long as you never use  
the mutable reference again.

> If the logic above is expressed as:
> Read bytes into an array
> Create a thread (task) to execute a function that takes a copy of 'bytes'
> Execute the thread
>
> I wonder if I could:
> Create a thread (task)
> Read bytes directly into the tasks' thread local storage
> Execute the thread

This *might* be possible.  However, in many cases, the OS is responsible  
for creating the TLS when the thread starts, so you have to wait until the  
thread is actually running to access it (not an expert on this, but I  
think this is the case for everything but OSX?)

So you would have to create the thread, have it pause while you fill it's  
TLS, then resume it.

But I think this is clearly a weird approach to this problem.  Finding a  
way to reliably pass the data to the sub-thread seems more appropriate.

BTW, I've dealt with having to access other threads' TLS.  It's not  
pretty, and I don't recommend using it except in specialized situations  
(mine was adding a GC hook).

-Steve


More information about the Digitalmars-d-learn mailing list