Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Demilivor opened this issue Dec 17, 2024 · 0 comments
Open

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

Demilivor opened this issue Dec 17, 2024 · 0 comments

Comments

@Demilivor
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant