Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
August 07, 2014 Passing Command Line Arguments to a new Thread | ||||
---|---|---|---|---|
| ||||
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." |
August 07, 2014 Re: Passing Command Line Arguments to a new Thread | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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. |
August 07, 2014 Re: Passing Command Line Arguments to a new Thread | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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.
|
August 07, 2014 Re: Passing Command Line Arguments to a new Thread | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | 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.
|
August 07, 2014 Re: Passing Command Line Arguments to a new Thread | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | 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.
|
August 07, 2014 Re: Passing Command Line Arguments to a new Thread | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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 |
August 07, 2014 Re: Passing Command Line Arguments to a new Thread | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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 |
August 07, 2014 Re: Passing Command Line Arguments to a new Thread | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johannes Blume | 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.
|
Copyright © 1999-2021 by the D Language Foundation