Waiting for a Spawn'ed process
Ali Çehreli
acehreli at yahoo.com
Fri Mar 28 10:37:58 PDT 2014
On 03/28/2014 10:19 AM, Sharad Gupta wrote:
> On Friday, 28 March 2014 at 16:37:00 UTC, Vladimir Panteleev wrote:
>> On Friday, 28 March 2014 at 15:52:17 UTC, Sharad Gupta wrote:
>>> I am trying to make a Utility which spwans=>pipeShell off multiple
>>> processes on user choice.
>>>
>>> On of the issues is that some process need to wait for other
>>> processes to finish. Now I could implement it using messages but that
>>> doesn't seem very nice solution. I could also wait for the pipeShell
>>> pid but was wondering what is the standard or recommended way to wait
>>> on a spawned process?
>>
>> Calling wait on the Pid object returned by std.process functions is
>> the standard way to wait for a process to finish in D.
>
> But I don't have a Pid object but instead have Tid object from the spawn
> function in std.concurrency lib.
If you start the worker with spawnLinked then you will receive a
LinkTerminated message.
import std.stdio;
import std.concurrency;
import core.thread;
void main()
{
auto worker = spawnLinked(&workerFunc);
// Wait for worker to terminate
bool terminated = false;
while (!terminated) {
writeln("Waiting for the worker to terminate...");
terminated = receiveTimeout(1.seconds,
(LinkTerminated e) {
if (e.tid == worker) {
writefln("Terminated");
}
}
);
}
}
void workerFunc()
{
Thread.sleep(3.seconds);
}
Ali
More information about the Digitalmars-d-learn
mailing list