www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Determination of thread status.

reply Vitaly <gkvetal gmail.com> writes:
Hi all.
I can not understand how to track me that the thread has finished 
work.
eg:

import std.concurrency;
void myThread ()
{
// Do the work
}

void main ()
{
Tid thread = spawn (& myThread);
// It is necessary to check whether the thread has finished its 
work or is active.
}

Could you tell me how to do this?
Dec 25 2018
next sibling parent reply Neia Neutuladh <neia ikeran.org> writes:
1. Find the Thread object:
  Tid threadId = spawn(&doStuff);
  auto thread = Thread.getAll.filter!(x => x.id == threadId).front;
2. Check the `isRunning` property.

The indirection with spawn() is awkward.
Dec 25 2018
parent reply Vitaly <gkvetal gmail.com> writes:
On Tuesday, 25 December 2018 at 17:08:00 UTC, Neia Neutuladh 
wrote:
 1. Find the Thread object:
   Tid threadId = spawn(&doStuff);
   auto thread = Thread.getAll.filter!(x => x.id == 
 threadId).front;
 2. Check the `isRunning` property.

 The indirection with spawn() is awkward.
Thanks for the answer. I checked. Thread.getAll [x] .id is of type ulong, and spawn is of type Tid, moreover, they do not intersect in value either. But even if the program spawns a single thread, Thread.getAll [x] .isRunning returns true after the function terminates. Perhaps you need to wait some time. You may have to send a message before exiting the function. Just look at the possibilities of std.parallelism. I will gladly try other solutions. I would be grateful for your suggestions.
Dec 25 2018
parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Wednesday, 26 December 2018 at 05:43:47 UTC, Vitaly wrote:
 On Tuesday, 25 December 2018 at 17:08:00 UTC, Neia Neutuladh 
 wrote:
 1. Find the Thread object:
   Tid threadId = spawn(&doStuff);
   auto thread = Thread.getAll.filter!(x => x.id == 
 threadId).front;
 2. Check the `isRunning` property.

 The indirection with spawn() is awkward.
Thanks for the answer. I checked. Thread.getAll [x] .id is of type ulong, and spawn is of type Tid, moreover, they do not intersect in value either. But even if the program spawns a single thread, Thread.getAll [x] .isRunning returns true after the function terminates. Perhaps you need to wait some time. You may have to send a message before exiting the function. Just look at the possibilities of std.parallelism. I will gladly try other solutions. I would be grateful for your suggestions.
Maybe use spawnLinked()? https://run.dlang.io/is/9xbyAF
Dec 26 2018
parent reply Vitaly <gkvetal gmail.com> writes:
 Maybe use spawnLinked()? https://run.dlang.io/is/9xbyAF
Thanks. At the moment, I implemented it through sending a message, and receiveTimeout receiving, if the response is not received after the timeout expires, then it is considered that the has terminated. Probably this is not right, but I will try other options.
Dec 26 2018
parent reply Brakeran <none none.nl> writes:
On Thursday, 27 December 2018 at 06:22:31 UTC, Vitaly wrote:
 Maybe use spawnLinked()? https://run.dlang.io/is/9xbyAF
Thanks. At the moment, I implemented it through sending a message, and receiveTimeout receiving, if the response is not received after the timeout expires, then it is considered that the has terminated. Probably this is not right, but I will try other options.
You could use (core.thread) register() the Tid and then locate() it, if return is 0 then the thread was done. else it is still alive doing something.
Dec 30 2018
parent Vitaly <gkvetal gmail.com> writes:
On Sunday, 30 December 2018 at 08:24:20 UTC, Brakeran wrote:
 On Thursday, 27 December 2018 at 06:22:31 UTC, Vitaly wrote:
 Maybe use spawnLinked()? https://run.dlang.io/is/9xbyAF
Thanks. At the moment, I implemented it through sending a message, and receiveTimeout receiving, if the response is not received after the timeout expires, then it is considered that the has terminated. Probably this is not right, but I will try other options.
You could use (core.thread) register() the Tid and then locate() it, if return is 0 then the thread was done. else it is still alive doing something.
Thank. It looks like it really is what you need. In the new year I will check. All happy new year in advance.
Dec 30 2018
prev sibling parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Tuesday, 25 December 2018 at 14:44:43 UTC, Vitaly wrote:
 Hi all.
 I can not understand how to track me that the thread has 
 finished work.
 eg:

 import std.concurrency;
 void myThread ()
 {
 // Do the work
 }

 void main ()
 {
 Tid thread = spawn (& myThread);
 // It is necessary to check whether the thread has finished its 
 work or is active.
 }

 Could you tell me how to do this?
std.concurrency is a low-level API. You may be looking for a higher level API: std.parallelism. See Task.done(), https://dlang.org/phobos/std_parallelism.html#.Task.done Bastiaan.
Dec 25 2018
next sibling parent Vitaly <gkvetal gmail.com> writes:
On Tuesday, 25 December 2018 at 17:12:13 UTC, Bastiaan Veelo 
wrote:
 std.concurrency is a low-level API. You may be looking for a 
 higher level API: std.parallelism. See Task.done(), 
 https://dlang.org/phobos/std_parallelism.html#.Task.done

 Bastiaan.
Thanks for the answer. I will look at the possibilities of std.parallelism.
Dec 25 2018
prev sibling parent Vitaly <gkvetal gmail.com> writes:
 std.concurrency is a low-level API. You may be looking for a 
 higher level API: std.parallelism. See Task.done(), 
 https://dlang.org/phobos/std_parallelism.html#.Task.done

 Bastiaan.
Maybe I misunderstood, but the capabilities of the std.parallelism module do not allow to exchange messages with the stream, if so, this does not suit me. Correct me if it is not.
Dec 25 2018