Thread overview
TDPL message passing: OwnerFailed?
Jan 22, 2013
monarch_dodra
Jan 22, 2013
Martin Drasar
Jan 22, 2013
Ali Çehreli
Jan 22, 2013
monarch_dodra
Jan 22, 2013
monarch_dodra
Jan 22, 2013
Ali Çehreli
January 22, 2013
I was trying to do a simple program to test message passing.

Basically, I have 1 owner, and 2 slave threads.

I'm facing two problems:

1 ) First, I want the slaves to be informed of when the master dies in an abnormal way.

TDPL suggest OwnerFailed, but apparently, the out of band exception that is thrown is an "OwnerTerminated", which does not allow me to know if the termination is "normal" or not.

Does that mean I have to manually send a message to my slaves to tell them master is finished, and have the slaves take "OwnerTerminated" as always abnormal?

2 ) Ditto, I need the master to be informed of when a slave dies (via exception).

However, no matter what I do, it would appear the master simply isn't informed of the death of its slaves. It just keeps running until it finishes, at which point it may and/or may not show the exception...

//----
import std.concurrency, std.stdio;
import core.thread;

void main()
{
    auto tid1 = spawn(&fileWriter1);
    auto tid2 = spawn(&fileWriter2);

    Thread.sleep(dur!"seconds"(1));

    send(tid1, 1);
    send(tid2, 2);

    stderr.writeln("Normally master.");
}

void fileWriter1() {
    throw new Exception("Bwah ha ha 1!");
}

void fileWriter2() {
    throw new Exception("Bwah ha ha 2!");
}
//----
Normally master.
//----

If I remove the sleep, I get:
//----
Normally master.
object.Exception@main.d(20): Bwah ha ha 2!
//----

FYI, the original project was writing a file writer that takes 1 input, and 2 outputs. THis is rather easy, but the error handling is eluding me.
January 22, 2013
On 22.1.2013 11:08, monarch_dodra wrote:
> I was trying to do a simple program to test message passing.
> 
> Basically, I have 1 owner, and 2 slave threads.
> 
> I'm facing two problems:
> 
> 1 ) First, I want the slaves to be informed of when the master dies in an abnormal way.
> 
> TDPL suggest OwnerFailed, but apparently, the out of band exception that is thrown is an "OwnerTerminated", which does not allow me to know if the termination is "normal" or not.
> 
> Does that mean I have to manually send a message to my slaves to tell them master is finished, and have the slaves take "OwnerTerminated" as always abnormal?
> 
> 2 ) Ditto, I need the master to be informed of when a slave dies (via
> exception).
> 
> However, no matter what I do, it would appear the master simply isn't informed of the death of its slaves. It just keeps running until it finishes, at which point it may and/or may not show the exception...
> 

Hi,

wouldn't this help you?

http://dlang.org/phobos/std_concurrency.html#.spawnLinked

According to documentation: Executes the supplied function in a new context represented by Tid. This new context is linked to the calling context so that if either it or the calling context terminates a LinkTerminated message will be sent to the other, causing a LinkTerminated exception to be thrown on receive(). The owner relationship from spawn() is preserved as well, so if the link between threads is broken, owner termination will still result in an OwnerTerminated exception to be thrown on receive().

Martin
January 22, 2013
On 01/22/2013 04:26 AM, Martin Drasar wrote:
> On 22.1.2013 11:08, monarch_dodra wrote:
>> I was trying to do a simple program to test message passing.

> wouldn't this help you?
>
> http://dlang.org/phobos/std_concurrency.html#.spawnLinked

The following chapter may be helpful as well:

  http://ddili.org/ders/d.en/concurrency.html

Especially the following sections:

- Exceptions during the execution of the worker

- Detecting thread termination

- Receiving exceptions as messages

Ali

January 22, 2013
On Tuesday, 22 January 2013 at 12:27:12 UTC, Martin Drasar wrote:
>
> Hi,
>
> wouldn't this help you?
>
> http://dlang.org/phobos/std_concurrency.html#.spawnLinked
>
> According to documentation: Executes the supplied function in a new
> context represented by Tid. This new context is linked to the calling
> context so that if either it or the calling context terminates a
> LinkTerminated message will be sent to the other, causing a
> LinkTerminated exception to be thrown on receive(). The owner
> relationship from spawn() is preserved as well, so if the link between
> threads is broken, owner termination will still result in an
> OwnerTerminated exception to be thrown on receive().
>
> Martin

Thanks. I tried using it, but it is giving me problems in the sense that:
1) If the worker sends a message, and then dies, the "LinkTerminated" message is sometimes not caught by the owner.
2) When the worker needs to die its natural death, it is creating some LinkTerminated errors :/

Gotta keep tinkering.

On Tuesday, 22 January 2013 at 14:47:10 UTC, Ali Çehreli wrote:
> On 01/22/2013 04:26 AM, Martin Drasar wrote:
> > On 22.1.2013 11:08, monarch_dodra wrote:
> >> I was trying to do a simple program to test message passing.
>
> > wouldn't this help you?
> >
> > http://dlang.org/phobos/std_concurrency.html#.spawnLinked
>
> The following chapter may be helpful as well:
>
>   http://ddili.org/ders/d.en/concurrency.html
>
> Especially the following sections:
>
> - Exceptions during the execution of the worker
>
> - Detecting thread termination
>
> - Receiving exceptions as messages
>
> Ali

TY. Gonna read everything.
January 22, 2013
On Tuesday, 22 January 2013 at 15:01:59 UTC, monarch_dodra wrote:
>
> TY. Gonna read everything.

I'm not enjoying this.

I'm getting some errors with this program:
//----
import std.stdio, std.file;
import std.concurrency;

void main(string[] args)
{
    auto iFile      = File("input.txt", "r");
    spawn(&fileWriter, "out1.txt");
    spawn(&fileWriter, "out2.txt");
}

void fileWriter(string fileName)
{
    File oFile = File(fileName, "w");
}
//----

There's a 50/50 chances it produces:
//----
std.exception.ErrnoException@std\stdio.d(467): Could not close file `input.txt' (No error)
//----
Always on the input files, never the outputs

I'm using a win32 dmd on win7_64.

The line in stdoo is:
//----
        errnoEnforce(.fclose(_p.handle) == 0,
                "Could not close file `"~_name~"'");
//----

I'd be tempted to say this is a bug... Maybe not a dmd/phobos bug, but I'm not sure how to work around it.
January 22, 2013
On 01/22/2013 09:04 AM, monarch_dodra wrote:

> I'm getting some errors with this program:

I can not reproduce this problem on Linux. Not with -m64 nor with -m32.

I don't think the following is necessary but it has been a workaround for me in the past:

import core.thread;

void main(string[] args)
{
// ...

    thread_joinAll();
}

Ali