Thread overview
One for experts in std.parallelism
Jun 12, 2017
Stanislav Blinov
Jun 12, 2017
Stanislav Blinov
Jun 12, 2017
Vladimir Panteleev
Jun 12, 2017
Moritz Maxeiner
June 11, 2017
I tried to eliminate the static shared ~this as follows:

https://github.com/dlang/phobos/pull/5470/commits/a4b2323f035b663727349d390719509d0e3247ba

However, unittesting fails at src/core/thread.d(2042). I suspect it's because the atexit call occurs too late, after the shared static this in src/core/thread.d has already been called.

Thoughts on how to make this work?


Thanks,

Andrei
June 12, 2017
On Sunday, 11 June 2017 at 19:06:48 UTC, Andrei Alexandrescu wrote:
> I tried to eliminate the static shared ~this as follows:
>
> https://github.com/dlang/phobos/pull/5470/commits/a4b2323f035b663727349d390719509d0e3247ba
>
> However, unittesting fails at src/core/thread.d(2042). I suspect it's because the atexit call occurs too late, after the shared static this in src/core/thread.d has already been called.
>
> Thoughts on how to make this work?
>
>
> Thanks,
>
> Andrei

Which atexit call? The shared static ~this is still present in that commit.
June 12, 2017
On 06/12/2017 12:13 AM, Stanislav Blinov wrote:
> On Sunday, 11 June 2017 at 19:06:48 UTC, Andrei Alexandrescu wrote:
>> I tried to eliminate the static shared ~this as follows:
>>
>> https://github.com/dlang/phobos/pull/5470/commits/a4b2323f035b663727349d390719509d0e3247ba 
>>
>>
>> However, unittesting fails at src/core/thread.d(2042). I suspect it's because the atexit call occurs too late, after the shared static this in src/core/thread.d has already been called.
>>
>> Thoughts on how to make this work?
>>
>>
>> Thanks,
>>
>> Andrei
> 
> Which atexit call? The shared static ~this is still present in that commit.

Eh, the link doesn't work anymore; I hoped it would be immutable. Anyhow, I just tried this again:

private final class ParallelismThread : Thread
{
    this(void delegate() dg)
    {
        super(dg);
        static shared bool once;
        import std.concurrency;
        initOnce!once({
            import core.stdc.stdlib;
            atexit(&doThisAtExit);
            return true;
        }());
    }

    TaskPool pool;
}

// Kill daemon threads.
//shared static ~this()
extern(C) void doThisAtExit()
{
    foreach (ref thread; Thread)
    {
        auto pthread = cast(ParallelismThread) thread;
        if (pthread is null) continue;
        auto pool = pthread.pool;
        if (!pool.isDaemon) continue;
        pool.stop();
        pthread.join();
    }
}

There's no more shared static ~this(). Instead, we have an atexit registration. It fails in src/core/thread.d(2042).


Andrei
June 12, 2017
On Monday, 12 June 2017 at 14:21:29 UTC, Andrei Alexandrescu wrote:
> On 06/12/2017 12:13 AM, Stanislav Blinov wrote:
>> On Sunday, 11 June 2017 at 19:06:48 UTC, Andrei Alexandrescu wrote:
>>> I tried to eliminate the static shared ~this as follows:
>>>
>>> https://github.com/dlang/phobos/pull/5470/commits/a4b2323f035b663727349d390719509d0e3247ba
>>>
>>>
>>> However, unittesting fails at src/core/thread.d(2042). I suspect it's because the atexit call occurs too late, after the shared static this in src/core/thread.d has already been called.
>>>
>>> Thoughts on how to make this work?
>>>
>>>
>>> Thanks,
>>>
>>> Andrei
>> 
>> Which atexit call? The shared static ~this is still present in that commit.
>
> Eh, the link doesn't work anymore; I hoped it would be immutable. Anyhow, I just tried this again:
>
> private final class ParallelismThread : Thread
> {
>     this(void delegate() dg)
>     {
>         super(dg);
>         static shared bool once;
>         import std.concurrency;
>         initOnce!once({
>             import core.stdc.stdlib;
>             atexit(&doThisAtExit);
>             return true;
>         }());
>     }
>
>     TaskPool pool;
> }
>
> // Kill daemon threads.
> //shared static ~this()
> extern(C) void doThisAtExit()
> {
>     foreach (ref thread; Thread)
>     {
>         auto pthread = cast(ParallelismThread) thread;
>         if (pthread is null) continue;
>         auto pool = pthread.pool;
>         if (!pool.isDaemon) continue;
>         pool.stop();
>         pthread.join();
>     }
> }
>
> There's no more shared static ~this(). Instead, we have an atexit registration. It fails in src/core/thread.d(2042).
>
>
> Andrei

To me, it feels like the options are:
1. Take a performance hit and unregister "parallel" threads from the runtime at creation, and keep a separate lock-protected list.
2. Modify runtime to allow creating unregistered Threads
3. Push thread_term() call back if at all possible (i.e. by also registering it with atexit).

I'll put the PR for (1) up for discussion a bit later. (2) and (3) I'll need to dig into runtime somewhat first.
June 12, 2017
On Monday, 12 June 2017 at 14:21:29 UTC, Andrei Alexandrescu wrote:
> On 06/12/2017 12:13 AM, Stanislav Blinov wrote:
>> On Sunday, 11 June 2017 at 19:06:48 UTC, Andrei Alexandrescu wrote:
>>> https://github.com/dlang/phobos/pull/5470/commits/a4b2323f035b663727349d390719509d0e3247ba
>
> Eh, the link doesn't work anymore; I hoped it would be immutable.

https://github.com/andralex/phobos/commit/a4b2323f035b663727349d390719509d0e3247ba

June 12, 2017
On 06/12/2017 01:43 PM, Stanislav Blinov wrote:
> 
> I'll put the PR for (1) up for discussion a bit later. (2) and (3) I'll need to dig into runtime somewhat first.

Thanks! -- Andrei
June 12, 2017
On Sunday, 11 June 2017 at 19:06:48 UTC, Andrei Alexandrescu wrote:
> I tried to eliminate the static shared ~this as follows:
>
> https://github.com/dlang/phobos/pull/5470/commits/a4b2323f035b663727349d390719509d0e3247ba
>
> However, unittesting fails at src/core/thread.d(2042). I suspect it's because the atexit call occurs too late, after the shared static this in src/core/thread.d has already been called.
>
> Thoughts on how to make this work?

Obligatory reminder that the static shared ~this you are trying to port is buggy[1] and any of std.parallelism's alleged "daemon" threads being busy in reality keeps the program from terminating.

[1] https://issues.dlang.org/show_bug.cgi?id=16324