On 08/08/2011 01:38 PM, Steven Schveighoffer wrote:
On Mon, 08 Aug 2011 14:17:28 -0400, Kai Meyer <[email protected]> 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

Well, bytes is in a loop, so casting to immutable wouldn't do it. The idea is to read a block of bytes, and hand them off to a worker thread to operate on those set of bytes. Everything is working, I'm just trying to avoid having to reallocate that block of bytes for the read, and then reallocate them again to pass them off to the worker thread. If I could get away with one allocation, I'd be happier.

-Kai Meyer

Reply via email to