digitalmars.D.learn - Passing Command Line Arguments to a new Thread
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (21/21) Aug 07 2014 What is the best way to forward a string[] as argument to a
- H. S. Teoh via Digitalmars-d-learn (5/29) Aug 07 2014 Maybe try args.idup instead?
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (5/38) Aug 07 2014 But this shouldn't be necessary, right? It's a mutable slice to
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (10/13) Aug 07 2014 I agree.
- Johannes Blume (8/11) Aug 07 2014 The elements of the slice itself are mutable, you can e.g. assign
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (2/14) Aug 07 2014 Ah, indeed. It's mutable ref to mutable ref to immutable data.
- Jacob Carlborg (4/6) Aug 07 2014 What about just accessing core.runtime.Runtime.args from the new thread?
- Jeremy DeHaan (5/27) Aug 07 2014 If you don't care how you get the args to otherMain, you can also
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
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
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: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.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?
Aug 07 2014
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
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
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:Ah, indeed. It's mutable ref to mutable ref to immutable data.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
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
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