www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Passing Command Line Arguments to a new Thread

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
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."
Aug 07 2014
next sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Thu, Aug 07, 2014 at 06:23:24PM +0000, "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.
Aug 07 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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 +0000, "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.
Aug 07 2014
next sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
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.
Aug 07 2014
prev sibling parent reply "Johannes Blume" <jblume jblume.com> writes:
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.
Aug 07 2014
parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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.
Aug 07 2014
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
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
Aug 07 2014
prev sibling parent "Jeremy DeHaan" <dehaan.jeremiah gmail.com> writes:
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.
Aug 07 2014