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?

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?

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.
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