www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19487] New: Thread never detaches after spawn

https://issues.dlang.org/show_bug.cgi?id=19487

          Issue ID: 19487
           Summary: Thread never detaches after spawn
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: undefer gmail.com

Program to test:


import std.stdio;
import std.concurrency;
import core.thread;
import core.memory;

void thread()
{
    writefln("Thread %s started", thisTid);
    Thread.sleep(10.seconds);
    writefln("Thread %s finished", thisTid);
}

void main()
{
    for (int i=0; i<1000; i++)
    {
        spawn(&thread);
    }
    Thread.sleep(13.seconds);
    writefln("Collect");
    GC.collect();
    Thread.sleep(10.seconds);
}

Run as ./threads.d
In other console do:
$ ps -e | grep threads
$ cat /proc/[PID]/maps | wc -l

It shows count of maps like 2479.
After "Collect" must be something like 243.
But it doesn't happen, because in the class Thread:

    ~this() nothrow  nogc
    {
        bool no_context = m_addr == m_addr.init;
        bool not_registered = !next && !prev && (sm_tbeg !is this);

        if (no_context || not_registered)
        {
            return;
        }

And after finishing of the thread it is always not_registered. Before finishing
the thread the dtor can't be run, because it is registered.

--
Dec 14 2018