Standalone threads

Lightoze Lightoze_member at pathlink.com
Sun Feb 19 11:01:38 PST 2006


In article <dt9v09$2bpf$1 at digitaldaemon.com>, Wolfgang Draxinger says...
>
>Lightoze wrote:
>
>> class X : Thread {
>> public this() {super(&(this.go));}
>> public ~this() {printf("Thread deleted\n");}
>> public int go() {printf("Thread started\n"); return 0;}
>> }
>> 
>> int main(char[][] args) {
>> while (true) {
>> X x = new X;
>> x.start();
>> //            x.wait();
>> }
>> }
>> 
>> Code above works fine when "x.wait()" line is uncommented, but
>> I dont want to wait X thread to end. If I comment this line
>> huge memory leaks occur. Please suggest any ways to solve this
>> problem, may be I need to add something at the end of go()
>> method of thread?
>
>You are aware, that above code will create threads endlessly if
>you don't wait for the thread to terminate? That explains, why
>the program sucks memory: Every thread has it's own stack and
>instance of class X.
>
>-- 
>Wolfgang Draxinger
>

I also use sleep(5) in this loop. So every new thread must be destroyed after
go() ends. But it is not.
I cannot download attachment from my previous post, so here it is:

private import std.thread;
private import std.stdio;
private import std.c.time;

class X : Thread {
public this() {
super(&(this.go));
}

public ~this() {
printf("Thread deleted\n");
}

public int go() {
printf("Thread started\n");
return 0;
}
}

int main(char[][] args) {
while (true) {
uint c = 0;
foreach (Thread t; Thread.getAll()) {
printf("Thread %i\n", t);
c++;
}
printf("Total threads: %i\n", c);
X x = new X;
x.start();

printf("PING: %i\n", time(null));
fflush(stdout);

sleep(5);
}
return 0;
}





More information about the Digitalmars-d mailing list