Forum Navigation
Simple Threads Crashing Randomly
Posted: 04/02/08 14:46:17Hi, could anyone tell me if I've got this code right or not. It some times exits correctly and sometimes segfaults at closing of the client.
its a server which says "hello world" over tcp then the client sends "hello back", the client creates multiple threads which connect to the server, the number is specified on the client command line.
The client or the server will randomly crash, to me the crash seems to happen inside tango.core.Thread.Thread.remove(Thread) or inside memcpy somewhere, using gdb. I am using Linux 2.6.
Server: import tango.core.Thread; import tango.io.Stdout; import tango.net.ServerSocket?,
tango.net.SocketConduit?;
void main() {
auto server = new ServerSocket?(new InternetAddress?(10000), 5, true); scope(exit) delete server;
uint conncount; while (true) {
auto request = server.accept(); //scope(exit) delete request;
auto handler = new Handler(request); handler.sequence = conncount; handler.start(); conncount++; Stdout(conncount).newline;
}
}
class Handler : Thread {
static char[] data = "Hello World!!!\r\n"; uint sent; uint received; uint sequence;
SocketConduit? request; this (SocketConduit? sc) {
this.request = sc;
super(&run);
// this.isDaemon = true;
}
void run() {
try {
do {
auto lensent = request.output.write(data); //Stdout("sent: ")(lensent).newline; sent++;
char[] buf; buf.length = 1024; auto len = request.input.read(buf); if (len == IConduit.Eof)
throw new Exception("Connection lost");
buf.length = len; //Stdout("got: ")(buf).newline; received++;
//if (sent %10000 == 0) // Stdout.format("#{} sent {} times", sequence, sent).newline;
} while (true);
} catch (Exception e) {
Stdout("Caught: ")(e).newline;
}
}
}
Client: import tango.core.Thread; import tango.io.Stdout; import tango.net.ServerSocket?,
tango.net.SocketConduit?;
import Integer = tango.text.convert.Integer; import tango.time.StopWatch?;
void main(char[][] args) {
if (args.length < 2)
throw new Exception("Count not specified: "~ args[0] ~" count");
uint numthreads = Integer.parse(args[1]); uint count; while (count < numthreads) {
(new Client()).start(); count++;
} Stdout("started: ")(count).newline;
}
class Client : Thread {
SocketConduit? client; StopWatch? stopwatch; this() {
client = new SocketConduit?(); client.connect(new InternetAddress?("localhost", 10000));
super(&run);
// this.isDaemon = true;
}
void run() {
try {
stopwatch.start(); do {
char[64] response; auto len = client.input.read(response); if (len == IConduit.Eof)
throw new Exception("Connection lost");
//Stdout("client got: ")(response[0..len]).newline;
if (stopwatch.microsec > 10_000_000) { // exit loop if we've been busy for 10 seconds
break;
}
auto sentlen = client.output.write("Hello Back!"); //Stdout("sent: ")(sentlen).newline;
} while (true);
} catch(Exception e) {
//Stdout("Caught: ")(e).newline;
} //Stdout("done").newline;
}
}