Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8510637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:45:35+00:00 2026-06-11T03:45:35+00:00

I am designing an asynchronous logger class as follows. However, not sure if I

  • 0

I am designing an asynchronous logger class as follows. However, not sure if I am using the boost condition variable in the right way. Can anyone comment on this? Here the processLogEntry method is a thread function and I am using boost here.

void LogWriter::stopThread()
{
    mStop = true;
    mCond.notify_one();
    mThread->join();
}   

void LogWriter::processLogEntry()
{
    while(!mStop)
    {
        boost::mutex::scoped_lock lock(mMutex);
        mCond.wait(lock);
        while(!q.empty())
        {
            // process begins
        }
    }
}

void LogWriter::addLogEntry()
{
    boost::mutex::scoped_lock lock(mMutex);
    // add it in the queue
    mCond.notify_one();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:45:37+00:00Added an answer on June 11, 2026 at 3:45 am

    As it has been pointed out, you must either make mStop atomic or guard all its accesses with the mutex. Forget about volatile, it’s not relevant to your purposes.

    Furthermore, when waiting on a condition variable a call to wait may return even if no notification functions were called (those are so-called spurious wake-ups). As such, calls to wait need to be guarded.

    void LogWriter::stopThread()
    {
        {
            boost::mutex::scoped_lock lock(mMutex);
            mStop = true;
            mCond.notify_one();
        }
        mThread->join();
    
    }   
    
    void LogWriter::processLogEntry()
    {
        for(;;) {
            boost::mutex::scoped_lock lock(mMutex);
            // We wait as long as we're not told to stop and
            // we don't have items to process
            while(!mStop && q.empty()) mCond.wait(lock);
    
            // Invariant: if we get here then
            // mStop || !q.empty() holds
    
            while(!q.empty())
            {
                // process begins
            }
    
            if(mStop) return;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm designing a networking framework which uses WSAEventSelect for asynchronous operations. I spawn one
When designing a form in the IDE, non-visual components (eg TMainMenus, TDatamodules) can be
While designing a new WPF application I noticed exceptions not being thrown during data
When designing both the domain-model and class-diagrams I am having some trouble understanding what
When designing an rdlc report using the VS 2010 report designer, report items such
In designing class with multiple/nested has-a relationships, who should catch exceptions? I guess this
I am designing an iOS app using XCode 4.2's storyboard feature. The app has
Not sure how to describe this for sure, but I think I've boiled down
I designing a polynomial class for one of my com sci courses , I
While designing the class, I always get confused what to put in Constructor and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.