Thread in detached state?
    Ali Çehreli via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Fri Nov 13 11:45:57 PST 2015
    
    
  
On 11/13/2015 07:35 AM, Ish wrote:
> I was directed here from General list, so be patient with me. I am
> looking for syntax for creating a detached-state thread in the spirit of
> POSIX thread attribute PTHREAD_CREATE_DETACHED (the thread resources are
> released on termination and not when the main thread terminates - allows
> for a very large number of threads).
>
> Sample code with call will be helpful.
I think the following is the idea:
import std.stdio;
import core.thread;
extern(C) void rt_moduleTlsDtor();
void threadFunc() {
     writeln("Worker thread started");
     thread_detachThis();
     scope(exit) rt_moduleTlsDtor();
     foreach (i; 0 .. 3) {
         Thread.sleep(1.seconds);
         writeln("Working...");
     }
     writeln("Worker terminating");
}
void main() {
     writeln("Creating thread in main");
     auto worker = new Thread(&threadFunc);
     writeln("Starting thread in main");
     worker.start();
     writeln("main terminating");
}
The output:
Creating thread in main
Starting thread in main
main terminating
Worker thread started
Working...
Working...
Working...
Worker terminating
Ali
    
    
More information about the Digitalmars-d-learn
mailing list