I think if i change second if to this:
else if(lLoopCounter == lLoopCount)
{
lExpected = 0;
std::unique_lock<std::mutex> lGuard(mMutex);
mCond.wait(
lGuard,
[&]()->bool { return std::atomic_load_explicit(&mFlag, std::memory_order_relaxed) == 0; });
}
and unlock to this:
std::lock_guard<std::mutex> lGuard(mMutex);
std::atomic_store_explicit(&mFlag, 0, std::memory_order_relaxed);
mCond.notify_one();
problem will solve
Now for optimization can i remove lGuard from unlock method and change std::memory_order_relaxed to std::memory_order_release? (I had doubts to post new question for this or not)