[Issue 16232] New: std.experimental.logger.core.sharedLog isn't thread-safe

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Jul 3 09:29:42 PDT 2016


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

          Issue ID: 16232
           Summary: std.experimental.logger.core.sharedLog isn't
                    thread-safe
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: ag0aep6g at gmail.com

std.experimental.logger.core.sharedLog effectively casts a Logger to/from
shared. So you get a Logger that's shared among threads but it's not typed as
shared.

The predefined loggers may have all their operations properly synchronized, but
a user-defined logger doesn't necessarily.

----
import core.thread: Thread;
import core.time: seconds;
import std.experimental.logger: Logger, LogLevel, sharedLog;

class MyLogger : Logger
{
    this() { super(LogLevel.info); }

    int x = 0;

    override void writeLogMsg(ref LogEntry payload) @safe
    {
        // exaggerated ++x
        int y = x;
        () @trusted { Thread.sleep(1.seconds); } ();
        x = y + 1;
    }
}

void main()
{
    auto myLogger = new MyLogger;
    sharedLog = myLogger;

    auto t = new Thread({ sharedLog.log(""); });
    t.start();

    // exaggerated ++myLogger.x
    int y = myLogger.x;
    Thread.sleep(2.seconds);
    myLogger.x = y + 1;

    t.join();
    assert(myLogger.x == 2); /* fails */
}
----

--


More information about the Digitalmars-d-bugs mailing list