Thread overview | ||||||
---|---|---|---|---|---|---|
|
April 10, 2016 Correct way to spawn many and stoping when one finishes ? | ||||
---|---|---|---|---|
| ||||
Is this corrrect ? Each task searches for the same thing so when once has found the others don't need to run anymore. It looks a bit strange not to stop those who havent find the thing: import std.concurrency, core.thread, std.random; void task() { size_t i; while (true) { Thread.sleep(dur!"nsecs"(rndGen.front / 100)); ++i; rndGen.popFront; if (i == 100) { send(ownerTid, true); break; } } } void main() { Tid s0 = spawn(&task); Tid s1 = spawn(&task); //... Tid sN = spawn(&task); receiveOnly!bool; } |
April 10, 2016 Re: Correct way to spawn many and stoping when one finishes ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to klimp | On Sunday, 10 April 2016 at 07:48:51 UTC, klimp wrote: > Is this corrrect ? Each task searches for the same thing so when once has found the others don't need to run anymore. It looks a bit strange not to stop those who havent find the thing: Actually I have to kill the other tasks, in this slightly modified try: import std.concurrency, core.thread, std.random, std.stdio; void task() { size_t i; while (true) { Thread.sleep(dur!"nsecs"(rndGen.front / 50)); ++i; rndGen.popFront; if (i == 100) { send(ownerTid, thisTid, true); writeln("thisT gonna write globals: ", thisTid); if (receiveOnly!bool) writeln("thisT writes globals: ", thisTid); send(ownerTid, true); break; } } } void main() { Tid t0 = spawn(&task); Tid t1 = spawn(&task); auto t = receiveOnly!(Tid, bool); if (t[0] == t1) send(t0, false); if (t[0] == t0) send(t1, false); if (t[1]) { send(t[0], true); receiveOnly!bool; return; } } I got as output: > thisT gonna write globals: Tid(7ff8d3035100) > thisT writes globals: Tid(7ff8d3035100) > thisT gonna write globals: Tid(7ff8d3035400) but I don't want the other spawned thread to continue until the end. I should only get: > thisT gonna write globals: Tid(7ff8d3035100) > thisT writes globals: Tid(7ff8d3035100) How can I kill a Tid ? |
April 12, 2016 Re: Correct way to spawn many and stoping when one finishes ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to klimp | On 4/10/16 4:59 AM, klimp wrote:
> On Sunday, 10 April 2016 at 07:48:51 UTC, klimp wrote:
>> Is this corrrect ? Each task searches for the same thing so when once
>> has found the others don't need to run anymore. It looks a bit strange
>> not to stop those who havent find the thing:
>
> Actually I have to kill the other tasks, in this slightly modified try:
>
> import std.concurrency, core.thread, std.random, std.stdio;
>
> void task()
> {
> size_t i;
> while (true)
> {
> Thread.sleep(dur!"nsecs"(rndGen.front / 50));
> ++i;
> rndGen.popFront;
> if (i == 100)
> {
> send(ownerTid, thisTid, true);
> writeln("thisT gonna write globals: ", thisTid);
> if (receiveOnly!bool)
> writeln("thisT writes globals: ", thisTid);
> send(ownerTid, true);
> break;
> }
> }
>
> }
>
> void main()
> {
> Tid t0 = spawn(&task);
> Tid t1 = spawn(&task);
>
> auto t = receiveOnly!(Tid, bool);
> if (t[0] == t1) send(t0, false);
> if (t[0] == t0) send(t1, false);
> if (t[1])
> {
> send(t[0], true);
> receiveOnly!bool;
> return;
> }
> }
>
> I got as output:
>
>> thisT gonna write globals: Tid(7ff8d3035100)
>> thisT writes globals: Tid(7ff8d3035100)
>> thisT gonna write globals: Tid(7ff8d3035400)
>
> but I don't want the other spawned thread to continue until the end.
>
> I should only get:
>
>> thisT gonna write globals: Tid(7ff8d3035100)
>> thisT writes globals: Tid(7ff8d3035100)
>
> How can I kill a Tid ?
Short answer: don't.
This is kind of why there isn't a handy function to do so.
If you kill a thread, there is no telling what state it is in, what locks it has held, etc.
The best (and really only) way to manage threads is through a loop that checks periodically whether it should quit.
-Steve
|
April 13, 2016 Re: Correct way to spawn many and stoping when one finishes ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Tuesday, 12 April 2016 at 12:12:19 UTC, Steven Schveighoffer wrote:
> On 4/10/16 4:59 AM, klimp wrote:
>> On Sunday, 10 April 2016 at 07:48:51 UTC, klimp wrote:
>>> Is this corrrect ? Each task searches for the same thing so when once
>>> has found the others don't need to run anymore. It looks a bit strange
>>> not to stop those who havent find the thing:
>> How can I kill a Tid ?
>
> Short answer: don't.
>
> This is kind of why there isn't a handy function to do so.
>
> If you kill a thread, there is no telling what state it is in, what locks it has held, etc.
>
> The best (and really only) way to manage threads is through a loop that checks periodically whether it should quit.
>
> -Steve
I've solved the problem by atomically reading/writing a shared bool.
That works fine, though I don't really need spawn() anymore. core.thread.Thread with a callback is widely enough.
|
Copyright © 1999-2021 by the D Language Foundation