Thread overview
Waiting for a Spawn'ed process
Mar 28, 2014
Sharad Gupta
Mar 28, 2014
Vladimir Panteleev
Mar 28, 2014
Sharad Gupta
Mar 28, 2014
Ali Çehreli
Mar 28, 2014
Sharad Gupta
Mar 28, 2014
Ali Çehreli
March 28, 2014
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?
March 28, 2014
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.
March 28, 2014
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.
March 28, 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

March 28, 2014
> 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

But this is another spawned process. How can I tell this second process to wait on the first one.

eg:

auto processA = spawnLinked(funcA);
auto processB = spawnLinked(funcB);

funcA()
{
  doSomething();
}

funcB()
{
  doSomething();
  // wait for A
  doSomethingElse();
}

This is a simplified example, the real case two process would be spawned irrespective to each other. the second process could may also be launched by when the first is already done.
March 28, 2014
On 03/28/2014 11:43 AM, Sharad Gupta wrote:

> But this is another spawned process. How can I tell this second process
> to wait on the first one.

std.concurrency is based on message passing. Normally, the second process would send a message either to its owner or to another thread that was previously register()'ed.

If message passing does not work for you then you may need to use lower-level modules like core.thread.

Ali