Skip to content

Commit

Permalink
fixed NGLMessage threading bug I hope
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacey committed Sep 26, 2023
1 parent 3301e21 commit d7232e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
37 changes: 19 additions & 18 deletions src/NGLMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ std::vector< Message > NGLMessage::s_messageQueue;
std::promise< void > g_exitSignal;
Colours NGLMessage::s_currentColour = Colours::NORMAL;
std::ofstream NGLMessage::s_logFile;

std::thread g_messageThread;
NGLMessage::NGLMessage()
{

Expand All @@ -25,27 +25,29 @@ NGLMessage::NGLMessage()
exit(EXIT_FAILURE);
}
s_futureExit = g_exitSignal.get_future();
std::thread t([this]()
{
while(s_futureExit.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout)
{
std::scoped_lock<std::mutex> lock(g_messageQueueLock);
if(! s_messageQueue.empty())
{
auto msg=s_messageQueue.back();
s_messageQueue.pop_back();
consume(msg);
}
} });
t.detach();
g_messageThread=std::thread ([this]()
{
while(s_futureExit.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout)
{
std::scoped_lock<std::mutex> lock(g_messageQueueLock);
if(! s_messageQueue.empty())
{
auto msg=s_messageQueue.back();
s_messageQueue.pop_back();
consume(msg);
}

} });
g_messageThread.detach();
}



NGLMessage::~NGLMessage()
{
while(! s_messageQueue.empty() )
while(!s_messageQueue.empty())
{
std::this_thread::sleep_for(std::chrono::microseconds(1));
}
g_exitSignal.set_value();
s_logFile.close();
}

Expand Down Expand Up @@ -148,7 +150,6 @@ std::string NGLMessage::getColourString(const Colours &_colour) const
return output;
}


void NGLMessage::addMessage(std::string_view _message, Colours _c, TimeFormat _timeFormat)
{
std::scoped_lock< std::mutex > lock(g_messageQueueLock);
Expand Down
2 changes: 1 addition & 1 deletion tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int main(int argc, char **argv)
if(useOpenGL == false)
{
std::cerr << "excluding tests\n";
::testing::GTEST_FLAG(filter) = "-ShaderLib.*:VAOPrimitives.*:NGLInit*:NGLMessage*";
::testing::GTEST_FLAG(filter) = "-ShaderLib.*:VAOPrimitives.*:NGLInit*";
}
// testing::internal::CaptureStdout();
testing::internal::CaptureStderr();
Expand Down

0 comments on commit d7232e1

Please sign in to comment.