www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Passing arguments to a new thread

reply "Mars" <- -.-> writes:
Hello everybody.
As the title states, I want to run a function in a new thread, 
and pass it some arguments. How would I do that? I guess I could 
make a class, deriving from Thread, and work with it, but out of 
curiosity, I'd like to know if it's possible with a simple 
function.

Mars
Jan 20 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 01/20/2012 03:12 PM, Mars wrote:
 Hello everybody.
 As the title states, I want to run a function in a new thread, and pass
 it some arguments. How would I do that? I guess I could make a class,
 deriving from Thread, and work with it, but out of curiosity, I'd like
 to know if it's possible with a simple function.

 Mars
See std.concurrency. auto tid = spawn(&function, arg1, arg2, arg3, ...);
Jan 20 2012
parent "Mars" <- -.-> writes:
On Friday, 20 January 2012 at 15:33:34 UTC, Timon Gehr wrote:
 On 01/20/2012 03:12 PM, Mars wrote:
 Hello everybody.
 As the title states, I want to run a function in a new thread, 
 and pass
 it some arguments. How would I do that? I guess I could make a 
 class,
 deriving from Thread, and work with it, but out of curiosity, 
 I'd like
 to know if it's possible with a simple function.

 Mars
See std.concurrency. auto tid = spawn(&function, arg1, arg2, arg3, ...);
Very interesting, thank you. I'm currently writing a little client<>server application, and wanted to use 1 thread per client, for simplicity, but with this it's probably not harder to just loop through non-blocking recvs, and pass something to a handler thread. Am I right?
Jan 20 2012