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

  • SEARCH
  • Home
  • 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 8868343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:15:00+00:00 2026-06-14T17:15:00+00:00

I am trying to write a class that would run a thread upon its

  • 0

I am trying to write a class that would run a thread upon its object creation and stop the thread once the object gets deleted.

class MyThread : public boost::thread {

public:

    MyThread() : bAlive(true) { 
        boost::thread(&MyThread::ThreadFunction,this);
    }

    ~MyThread() {
        {
            boost::unique_lock<boost::mutex> lock(Mutex);
            bAlive=false;
        }
        ConditionVariable.notify_one();
        join();
    }

private:

    volatile bool bAlive;
    boost::mutex Mutex;
    boost::condition_variable ConditionVariable;

    void ThreadFunction() {
        boost::unique_lock<boost::mutex> lock(Mutex);
        while(bAlive) {
            ConditionVariable.timed_wait(lock,boost::get_system_time()+ boost::posix_time::milliseconds(MAX_IDLE));

            /*******************************************
            * Here goes some code executed by a thread * 
            *******************************************/

        }
    }

};

Theoretically, I want to wake the thread up instantly as soon as it needs to be finished, so I had to use timed_wait instead of Sleep.
This works fine until I try to delete an object of this class. In most cases, it deletes normally, but occasionally it causes an error either in condition_variable.hpp, thread_primitives.hpp or crtexe.c. Sometimes I am notified that “Free Heap block 3da7a8 modified at 3da804 after it was freed”, and sometimes I’m not. And yes, I’m aware of the spurious wakeups of timed_wait, in this case it’s not critical.
Can you please point me to the source of my problem? What am I doing wrong?

  • 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-14T17:15:02+00:00Added an answer on June 14, 2026 at 5:15 pm

    I see what you’re trying to do but it doesn’t work as you expect:

    MyThread foo;
    

    default constructs a boost::thread (because MyThread is derived from boost::thread).
    The default constructor creates a boost::thread instance that refers to Not-a-Thread.

    MyThread() {
        boost::thread(&MyThread::ThreadFunction,this);
    }
    

    is actually creating a different thread and you’re ignoring the returned object (the valid thread).

    ~MyThread() {
        // ...
        join();
    }
    

    is then trying to join the default constructed thread (which throws an exception inside the destructor) and you never join the thread that actually does the work.


    First of all, don’t derive from boost::thread. Create a member variable instead:

    class MyThread {
    // ...
    private:
        // ...
        boost::thread _thread;
    };
    

    In the constructor, create and assign a thread to that member variable:

    MyThread() {
        _thread = boost::thread(&MyThread::ThreadFunction,this);
    }
    

    and call its join() in your destructor.

    ~MyThread() {
        // ...
        _thread.join();
    }
    

    That should fix your problem.


    However, if you simply want to exit the thread when your object is destroyed (and don’t have to wake it up while its running), you can use a different approach. Remove the mutex and the condition variable and use interrupt instead. This will cause sleep() to throw an exception so you have to catch it:

    void ThreadFunction() {
        try {
            for(;;) {
                boost::this_thread::sleep(boost::posix_time::milliseconds(MAX_IDLE));
                // Here goes some code executed by a thread
            }
        } catch( const boost::thread_interrupted& e ) {
            // ignore exception: thread interrupted, exit function
        }
    }
    

    This will instantly exit the ThreadFunction when the thread is interrupted. If you don’t need the thread to sleep every cycle, you can replace it with boost::this_thread::interruption_point(). This will just throw an exception if the thread is interrupted.

    Now you can simply interrupt the thread in the destructor:

    MyThread::~MyThread() {
        _thread.interrupt();
        _thread.join();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write class that inherits from FMX TStyledControl. When style is
I'm trying to write a class that will return a string of HTML with
I've been trying to write an error handling class that I can use on
I am trying to write a Unit test for a class that has several
I'm trying to write a code that adds a class to a div for
I'm trying to write a named scope that will order my 'Products' class based
I am trying to write a method that returns a methodinfo class from a
I'm trying to write a class that encapsulates WCF calls (the client is Silverlight,
I was trying to write a matrix class which would be able to find
I'm trying to write a class in C++, but whenever I try to compile,

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.