www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Non-GC threads

reply Piotr Szturmaj <bncrbme jadamspam.pl> writes:
Hi,

What are the consequences of using non garbage collected threads in D?

I want to write a reliable communication protocol, but I don't want 
suspend this code by the GC.

Is there any method to bind allocated memory to thread itself, so it 
will be freed after thread terminates, but not in the middle of execution?

This is important because in the other case, GC could free memory 
referenced by non-GC thread.

Piotr
May 15 2011
parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Sun, 15 May 2011 21:30:32 +0300, Piotr Szturmaj <bncrbme jadamspam.pl>  
wrote:

 What are the consequences of using non garbage collected threads in D?
If you move pointers around while the GC is looking for them, the GC might miss them and free referenced memory. This will lead to memory corruption. The GC will also not be able to scan the stack and registers of threads it doesn't know about.
 I want to write a reliable communication protocol, but I don't want  
 suspend this code by the GC.
The GC might be much faster than you think it is. You should try benchmarking the GC with a typical memory load, the delay might be acceptable for your purposes.
 Is there any method to bind allocated memory to thread itself, so it  
 will be freed after thread terminates, but not in the middle of  
 execution?
You can use malloc/free together with RAII. Or something hacky and platform-dependent like pthread_cleanup_push().
 This is important because in the other case, GC could free memory  
 referenced by non-GC thread.
I can think of no perfect solution for your case. You'll either need to give up on using managed memory, or accept the periodical delays of the GC. -- Best regards, Vladimir mailto:vladimir thecybershadow.net
May 17 2011