www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Standalone threads

reply Lightoze <Lightoze_member pathlink.com> writes:
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?
Feb 17 2006
next sibling parent reply Sean Kelly <sean f4.ca> writes:
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?
If you don't wait for the thread to end then the app will just terminate and kill the thread in the middle of whatever it's doing. Sean
Feb 17 2006
parent reply Lightoze <Lightoze_member pathlink.com> writes:
In article <dt689o$183a$1 digitaldaemon.com>, Sean Kelly 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?
If you don't wait for the thread to end then the app will just terminate and kill the thread in the middle of whatever it's doing. Sean
Why do you think so? See example http://users.actcom.co.il/~choo/lupg/tutorials/multi-thread/pthread_create.c, do you know how to do this in D? I have read std.thread source, there are no calls of pthread_exit calls or something else for this purpose, so internal thread data cannot be freed.
Feb 18 2006
parent reply Sean Kelly <sean f4.ca> writes:
Lightoze wrote:
 In article <dt689o$183a$1 digitaldaemon.com>, Sean Kelly 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?
If you don't wait for the thread to end then the app will just terminate and kill the thread in the middle of whatever it's doing.
Why do you think so? See example http://users.actcom.co.il/~choo/lupg/tutorials/multi-thread/pthread_create.c, do you know how to do this in D?
Oops, I missed that your sample code is in an endless loop. It shouldn't be necessary to call wait() because the main thread should never actually terminate. The memory for these threads should eventually be collected by the GC unless there's a bug in the thread code.
 I have read std.thread source, there are no calls of pthread_exit calls or
 something else for this purpose, so internal thread data cannot be freed.
It's never actually necessary to call pthread_exit. In fact, I discourage its use as it's similar to calling the exit() C library function--doing so bypasses cleanup code and exits the thread immediately (three is actually a way to attach an onExit callback to pthreads, but this is more of a failsafe measure, and I don't think there's an equivalent for Windows threads). If you really want to exit a thread midstream you're better off throwing an Exception, as it will ensure auto objects are cleaned up, finally blocks are run, etc. Sean
Feb 18 2006
parent reply Lightoze <Lightoze_member pathlink.com> writes:
In article <dt85j8$2vho$1 digitaldaemon.com>, Sean Kelly says...
Lightoze wrote:
 In article <dt689o$183a$1 digitaldaemon.com>, Sean Kelly 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?
If you don't wait for the thread to end then the app will just terminate and kill the thread in the middle of whatever it's doing.
Why do you think so? See example http://users.actcom.co.il/~choo/lupg/tutorials/multi-thread/pthread_create.c, do you know how to do this in D?
Oops, I missed that your sample code is in an endless loop. It shouldn't be necessary to call wait() because the main thread should never actually terminate. The memory for these threads should eventually be collected by the GC unless there's a bug in the thread code.
 I have read std.thread source, there are no calls of pthread_exit calls or
 something else for this purpose, so internal thread data cannot be freed.
It's never actually necessary to call pthread_exit. In fact, I discourage its use as it's similar to calling the exit() C library function--doing so bypasses cleanup code and exits the thread immediately (three is actually a way to attach an onExit callback to pthreads, but this is more of a failsafe measure, and I don't think there's an equivalent for Windows threads). If you really want to exit a thread midstream you're better off throwing an Exception, as it will ensure auto objects are cleaned up, finally blocks are run, etc. Sean
I do not want to exit a thread midstream, but I want detach it from main thread, as in examples. I want to run many child threads (not only at startup) , but I do not know when they end. Using code above causes memory leaks, and I ask _how_ to write it correctly. If you just explain _how_ it would be good. Working code will be the best. Thanks.
Feb 18 2006
parent reply Sean Kelly <sean f4.ca> writes:
Lightoze wrote:
 
 I do not want to exit a thread midstream, but I want detach it from main
thread,
 as in examples. I want to run many child threads (not only at startup) , but I
 do not know when they end. Using code above causes memory leaks, and I ask
_how_
 to write it correctly. If you just explain _how_ it would be good. Working code
 will be the best.
What you're doing should be fine. How are you detecting that a memory leak is occurring? Sean
Feb 18 2006
parent Lightoze <Lightoze_member pathlink.com> writes:
What you're doing should be fine.  How are you detecting that a memory 
leak is occurring?

Sean
I use "top" command on my linux. For testing I execute sample code with delay in loop. Resident size grows by 8kb per iteration, virtual size grows by 8mb+ per iteration. I have virtual size over 300mb after several minutes. I have attached test program I use - you can try it. begin 0644 test.d M<')I=F%T92!I;7!O<G0 <W1D+G1H<F5A9#L*<')I=F%T92!I;7!O<G0 <W1D M+G-T9&EO.PIP<FEV871E(&EM<&]R="!S=&0N8V]N=CL*<')I=F%T92!I;7!O M<G0 <W1D+F,N=&EM93L*"F-L87-S(% .B!4:')E860 >PH)<'5B;&EC('1H M:7,H*2![" D)<W5P97(H)BAT:&ES+F=O*2D[" E]" D*"7!U8FQI8R!^=&AI M<R I('L*"0EP<FEN=&8H(E1H<F5A9"!D96QE=&5D7&XB*3L*"7T*"0H)<'5B M;&EC(&EN="!G;R I('L*"0EP<FEN=&8H(E1H<F5A9"!S=&%R=&5D7&XB*3L* M"0ER971U<FX ,#L*"7T*?0H*:6YT(&UA:6XH8VAA<EM=6UT 87)G<RD >PH) M=VAI;&4 *'1R=64I('L*"0EU:6YT(&, /2`P.PH)"69O<F5A8V *%1H<F5A M9"!T.R!4:')E860N9V5T06QL*"DI('L*"0D)<')I;G1F*")4:')E860 )6E< M;B(L('0I.PH)"0EC*RL[" D)?0H)"7!R:6YT9B B5&]T86P =&AR96%D<SH M)6E<;B(L(&,I.PH)"5 >"`](&YE=R!8.PH)"7 N<W1A<G0H*3L*"0D*"0EP M<FEN=&8H(E!)3D<Z("5I7&XB+"!T:6UE*&YU;&PI*3L*"0EF9FQU<V H<W1D D;W5T*3L*"0D*"0ES;&5E<" U*3L*"7T*"7)E='5R;B`P.PI] ` end
Feb 19 2006
prev sibling parent reply Wolfgang Draxinger <wdraxinger darkstargames.de> writes:
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
Feb 19 2006
parent Lightoze <Lightoze_member pathlink.com> writes:
In article <dt9v09$2bpf$1 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; }
Feb 19 2006