Re: Passing Command Line Arguments to a new Thread

2014-08-07 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Aug 07, 2014 at 06:23:24PM +, Nordlöw via Digitalmars-d-learn 
wrote:
 What is the best way to forward a string[] as argument to a function
 called through std.concurrency.spawn().
 
 I need this in the following example where I start the vibe.d event
 loop in the main thread (the only way I've managed to get
 runEventLoop() to work) and run my other program logic in another
 which requires command line arguments to passed to the new thread.
 
 void otherMain(string[] args)
 {
 // use args
 }
 
 void main(string[] args)
 {
 import std.concurrency: spawn;
 auto otherMainTid = spawn(otherMain, args); // this line fails
 runEventLoop();
 }
 
 The line calling spawn() fails as
 
 /home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/concurrency.d(442):
 Error: static assert  Aliases to mutable thread-local data not allowed.

Maybe try args.idup instead?


T

-- 
2+2=4. 2*2=4. 2^2=4. Therefore, +, *, and ^ are the same operation.


Re: Passing Command Line Arguments to a new Thread

2014-08-07 Thread via Digitalmars-d-learn
On Thursday, 7 August 2014 at 18:33:40 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
On Thu, Aug 07, 2014 at 06:23:24PM +, Nordlöw via 
Digitalmars-d-learn wrote:
What is the best way to forward a string[] as argument to a 
function

called through std.concurrency.spawn().

I need this in the following example where I start the vibe.d 
event

loop in the main thread (the only way I've managed to get
runEventLoop() to work) and run my other program logic in 
another
which requires command line arguments to passed to the new 
thread.


void otherMain(string[] args)
{
// use args
}

void main(string[] args)
{
import std.concurrency: spawn;
auto otherMainTid = spawn(otherMain, args); // this line 
fails

runEventLoop();
}

The line calling spawn() fails as

/home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/concurrency.d(442):
Error: static assert  Aliases to mutable thread-local data 
not allowed.


Maybe try args.idup instead?


But this shouldn't be necessary, right? It's a mutable slice to 
immutable data, but the slice is passed by value, so no mutable 
sharing takes place.


Re: Passing Command Line Arguments to a new Thread

2014-08-07 Thread Nordlöw

On Thursday, 7 August 2014 at 18:38:37 UTC, Marc Schütz wrote:
But this shouldn't be necessary, right? It's a mutable slice to 
immutable data, but the slice is passed by value, so no mutable 
sharing takes place.


I agree.

I'll use .idup anyhow. For this work I however have to do

void otherMain(immutable string[] args)
{
 useArgs(args.dup);
}

as my function useArgs has signature

useArgs(string[] args)

Seems awkward.


Re: Passing Command Line Arguments to a new Thread

2014-08-07 Thread Johannes Blume via Digitalmars-d-learn

On Thursday, 7 August 2014 at 18:38:37 UTC, Marc Schütz wrote:
But this shouldn't be necessary, right? It's a mutable slice to 
immutable data, but the slice is passed by value, so no mutable 
sharing takes place.


The elements of the slice itself are mutable, you can e.g. assign 
some other string to args[1], which would be a potentially racy 
change among all threads which share that slice. Only the 
information start address of slice and length of slice are 
copied by value, which doesn't protect from this.


To be able to share the slice, it would need to be typed as 
immutable(string)[] instead.


Re: Passing Command Line Arguments to a new Thread

2014-08-07 Thread Jacob Carlborg via Digitalmars-d-learn

On 2014-08-07 20:23, Nordlöw wrote:

What is the best way to forward a string[] as argument to a function
called through std.concurrency.spawn().


What about just accessing core.runtime.Runtime.args from the new thread?

--
/Jacob Carlborg


Re: Passing Command Line Arguments to a new Thread

2014-08-07 Thread Jeremy DeHaan via Digitalmars-d-learn

On Thursday, 7 August 2014 at 18:23:26 UTC, Nordlöw wrote:
What is the best way to forward a string[] as argument to a 
function called through std.concurrency.spawn().


I need this in the following example where I start the vibe.d 
event loop in the main thread (the only way I've managed to get 
runEventLoop() to work) and run my other program logic in 
another which requires command line arguments to passed to the 
new thread.


void otherMain(string[] args)
{
// use args
}

void main(string[] args)
{
import std.concurrency: spawn;
auto otherMainTid = spawn(otherMain, args); // this line 
fails

runEventLoop();
}

The line calling spawn() fails as

/home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/concurrency.d(442): 
Error: static assert  Aliases to mutable thread-local data not 
allowed.


If you don't care how you get the args to otherMain, you can also 
use Runtime.args. That way you wouldn't even need to pass it to 
the function in the first place.



http://dlang.org/phobos/core_runtime.html#.Runtime.args





Re: Passing Command Line Arguments to a new Thread

2014-08-07 Thread via Digitalmars-d-learn

On Thursday, 7 August 2014 at 19:08:37 UTC, Johannes Blume wrote:

On Thursday, 7 August 2014 at 18:38:37 UTC, Marc Schütz wrote:
But this shouldn't be necessary, right? It's a mutable slice 
to immutable data, but the slice is passed by value, so no 
mutable sharing takes place.


The elements of the slice itself are mutable, you can e.g. 
assign some other string to args[1], which would be a 
potentially racy change among all threads which share that 
slice. Only the information start address of slice and 
length of slice are copied by value, which doesn't protect 
from this.


To be able to share the slice, it would need to be typed as 
immutable(string)[] instead.


Ah, indeed. It's mutable ref to mutable ref to immutable data.