Strange exception using threads

Minas Mina minas_mina1990 at hotmail.co.uk
Sat Jun 23 09:05:47 PDT 2012


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?


More information about the Digitalmars-d-learn mailing list