Strange exception using threads
simendsjo
simendsjo at gmail.com
Sat Jun 23 09:29:37 PDT 2012
On Sat, 23 Jun 2012 18:05:47 +0200, Minas Mina
<minas_mina1990 at hotmail.co.uk> wrote:
> I am using a secondary thread to send messages to it so it can print
> those messages.
>
> import std.stdio;
> import std.concurrency;
>
> void main()
> {
> auto low = 0, high = 10;
>
> auto tid = spawn(&writer);
>
> foreach(i; low..high)
> {
> writeln("Main thread: ", i);
> tid.send(tid, i);
> }
> }
>
> void writer()
> {
> while( true )
> {
> receive(
> (Tid id, int i)
> {
> writeln("Secondary thread: ", i);
> }
> );
> }
> }
>
> This is the result:
> Main thread: 0
> Main thread: 1
> Main thread: 2
> Main thread: 3
> Main thread: 4
> Main thread: 5
> Main thread: 6
> Main thread: 7
> Main thread: 8
> Main thread: 9
> Secondary thread: 0
> Secondary thread: 1
> Secondary thread: 2
> Secondary thread: 3
> Secondary thread: 4
> Secondary thread: 5
> Secondary thread: 6
> Secondary thread: 7
> Secondary thread: 8
> Secondary thread: 9
> std.concurrency.OwnerTerminated at std/concurrency.d(248): Owner terminated
> ----------------
> /home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(bool
> std.concurrency.MessageBox.get!(void function(std.concurrency.Tid,
> int)*).get(scope void function(std.concurrency.Tid, int)*).bool
> onControlMsg(ref std.concurrency.Message)+0x2a) [0x437016]
> /home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(bool
> std.concurrency.MessageBox.get!(void function(std.concurrency.Tid,
> int)*).get(scope void function(std.concurrency.Tid, int)*).bool scan(ref
> std.concurrency.List!(std.concurrency.Message).List)+0x68) [0x437084]
> /home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(bool
> std.concurrency.MessageBox.get!(void function(std.concurrency.Tid,
> int)*).get(scope void function(std.concurrency.Tid, int)*)+0x88)
> [0x436c20]
> /home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(void
> std.concurrency.receive!(void function(std.concurrency.Tid,
> int)*).receive(void function(std.concurrency.Tid, int)*)+0x32) [0x436b86]
> /home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(void
> main.writer()+0x13) [0x43066b]
> /home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(_D3std11concurrency11__T6_spawnZ6_spawnFbPFZvZS3std11concurrency3Tid4execMFZv+0x45)
> [0x430941]
> /home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(void
> core.thread.Thread.run()+0x2a) [0x4470fe]
> /home/minas/Projects/D/D_Test/D_Test/bin/Debug/D_Test(thread_entryPoint+0xf3)
> [0x446e97]
>
> The program runs correctly but then boom! Does anyone know why?
You can notify child threads that their owner terminates so they can
finish up
import std.stdio;
import std.concurrency;
void main()
{
auto low = 0, high = 10;
auto tid = spawnLinked(&writer);
foreach(i; low..high)
{
writeln("Main thread: ", i);
tid.send(tid, i);
}
writeln("need to gracefully terminate child threads");
tid.send(tid, Term());
writeln("term sent");
}
struct Term {}
void writer()
{
bool done;
while( !done )
{
receive(
(Tid id, int i)
{
writeln("Secondary thread: ", i);
},
(Tid id, Term term)
{
writeln("Owner terminated, so do we");
done = true;
}
);
}
}
Main thread: 0
Main thread: 1
Main thread: 2
Main thread: 3
Main thread: 4
Secondary thread: 0
Secondary thread: 1
Secondary thread: 2
Secondary thread: 3
Secondary thread: 4
Main thread: 5
Main thread: 6
Main thread: 7
Main thread: 8
Main thread: 9
Secondary thread: 5
Secondary thread: 6
need to gracefully terminate child threads
term sent
Secondary thread: 7
Secondary thread: 8
Secondary thread: 9
Owner terminated, so do we
More information about the Digitalmars-d-learn
mailing list