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

Update Logger.cpp #51

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,18 @@ LogUnit* Logger::getLogUnit()
}
} else {
std::unique_lock<std::mutex> lck(mMtx);
if (!mFree.empty()) {
log = mFree.back();
mFree.resize(mFree.size() - 1);
} else if (mLogUnitCnt < mFree.capacity()) {
++mLogUnitCnt;
} else {
while (mFree.empty() && !mStop) {
mCond.wait(lck);
}
while (!mStop) {
if (!mFree.empty()) {
log = mFree.back();
mFree.resize(mFree.size() - 1);
break;
} else if (mLogUnitCnt < mFree.capacity()) {
++mLogUnitCnt;
break;
} else {
return nullptr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only condtion arrive here is mStop==true, so we should abandon this log then quit asap

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current worker thread may also be woken up when other worker threads call the Logger::gInst::put method. Because two queues share the same condition variable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that two queues should each have their own locks and condition variables instead of sharing the same lock and the same condition variable.

while (mFree.empty() && !mStop) {
mCond.wait(lck);
}
}
}
}
Expand Down