Am Tue, 10 Dec 2013 20:36:11 +0100 schrieb "Gianni Pisetta" <pisetta.gia...@alice.it>:
> On Tuesday, 10 December 2013 at 17:14:47 UTC, Ali Çehreli wrote: > > On 12/10/2013 06:56 AM, Gianni Pisetta wrote: > >> On Tuesday, 10 December 2013 at 13:39:17 UTC, Ali Çehreli > >> wrote: > >>> On 12/10/2013 04:04 AM, Gianni Pisetta wrote: > >>>> Hi, > >>>> i want to duplicate/clone a thread with the stack and tls, > >>>> like the > >>>> standard function fork() in c. Is in the phobos library a > >>>> method that do > >>>> the same thing? > >>>> > >>>> Gianni Pisetta > >>> > >>> Apparently, there isn't. However, you can use fork() directly > >>> from the > >>> core.sys.posix.unistd module. > >>> > >>> Ali > >> > >> I had the same idea, but assumed (maybe wrongly) that it > >> messes up the > >> garbage collection because it creates a copy of the allocated > >> objects in > >> tls that the gc is not aware of. I searched the core.thread > >> reference to > >> see if it has a similar method but found none. Then i posted > >> this > >> question. So is it safe to use fork()? I now realized that i > >> don't know > >> how the gc works. > >> > >> Gianni Pisetta > > > > I don't know such details but I presume fork() is safe. > > Grepping in Phobos modules, process.d uses it. > > > > Ali > > Ok, thanks for the reply. I will give it a try. > > Gianni Pisetta D is cross-platform, so to have a wrapper for fork() would mean to get it working on Windows, too. std.process, can must use fork() of course, since it is the way to spawn a child process on Posix systems. fork() doesn't clone a thread, but a whole process. So there is no issue with the GC, as it will be cloned as well. Until Posix threads were introduced, it was common to create "threads" using fork(), so it probably works ok for you. Maybe we can find a better solution though if you tell us some more. For example this exists in D: static this() { // Initialize TLS variables } That is a module constructor that is run once for each new Thread. You could prepare your TLS variables there as a copy of global data or other function calls. If you really need an automated copy of all TLS variables for a new thread the situation looks dim I guess :p -- Marco