Skip to content

Make trantor::Logger::setOutputFunction thread safe #364

@Demilivor

Description

@Demilivor

The call to the trantor::Logger::setOutputFunction is not thread safe:

static void setOutputFunction(
std::function<void(const char *msg, const uint64_t len)> outputFunc,
std::function<void()> flushFunc,
int index = -1)
{
if (index < 0)
{
outputFunc_() = outputFunc;
flushFunc_() = flushFunc;
}
else
{
outputFunc_(index) = outputFunc;
flushFunc_(index) = flushFunc;
}
}

2 threads can simultaneously set the values, or at least 1 simulaneous reader can read the value during the change of output function.

Proposal solution:
Use std::shared_mutex here. Use exclusive lock during modification and shared lock during the reading. This sure will consume some performance, but at least will be thread-safe.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions