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 7054983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:41:36+00:00 2026-05-28T03:41:36+00:00

Since I’ve started making a little project aiming to have a crossplatform support, I

  • 0

Since I’ve started making a little project aiming to have a crossplatform support, I chose boost 1.47 to interact with the underlying OS. My project needed some multithreading, so I made a little wrapper over boost threads to fulfill my needs.

Little I knew, boost apparently leaves the thread on memory after destructing its object(?), or then it may have some sort of memory leak possibility.

The implementation of my wrapper has a scoped_ptr of type thread, and the scoped ptr will get initialized when one calls the start() function in the wrapper class. The running thread will be stopped from main thread using thread->interrupt(), and the destructor will be called from the wrapper function. (Destructor of the thread’s procedure structure, which has operator()() in it.

Here’s the implementation of the wrapper class:
(note: i_exception and couple of other functions are parts of other project components)

#define TIMED_JOIN boost::posix_time::milliseconds(1)

namespace utils
{
    struct thread_threadable
    {
        template<typename T> friend class ut_thread;
    private:
        boost::shared_ptr<thread_threadable> instance;
    public:
        virtual ~thread_threadable() {}
        virtual void operator()() = 0;
    };

    template<typename T = thread_threadable>
    class ut_thread
    {
    public:
        typedef T proc_t;
    private:
        boost::scoped_ptr<boost::thread> thr;
        boost::shared_ptr<proc_t> proc;
    public:
        explicit ut_thread(const boost::shared_ptr<proc_t> &procedure) : proc(procedure) {}
        ~ut_thread();

        void start();
        void stop();

        bool running() const {return this->thr.get() != NULL;}
        proc_t &procedure() const
        {
            BOOST_ASSERT(this->proc.get() != NULL);
            return *this->proc;
        }
    };
}
typedef utils::thread_threadable threadable;

template<typename T>
utils::ut_thread<T>::~ut_thread()
{
    if(this->thr.get() != NULL)
    {
        BOOST_ASSERT(this->proc.get() != NULL);
        this->stop();
    }
}
template<typename T>
void utils::ut_thread<T>::start()
{
    if(this->thr.get() != NULL)
        i_exception::throw_this("another thread of this procedure is already running");
    if(this->proc.get() == NULL)
        i_exception::throw_this("procedure object not initialized");

    this->proc->instance = this->proc;

    this->thr.reset(new boost::thread(boost::ref(*this->proc)));
    this->thr->timed_join(TIMED_JOIN);
}
template<typename T>
void utils::ut_thread<T>::stop()
{
    if(this->thr.get() == NULL)
        i_exception::throw_this("no thread was running");

    this->thr->interrupt();
    this->proc->~T();
    this->thr.reset(NULL);
}

And then by checking the functionality of this wrapper class, I made test to main.cpp:

struct my_thr : public utils::thread_threadable
{
    void operator()()
    {
        while(true);
    }
};

int main()
{
    while(true)
    {
        utils::ut_thread<> thr(boost::shared_ptr<threadable>(new my_thr));
        utils::ut_thread<> thr1(boost::shared_ptr<threadable>(new my_thr));
        thr.start();
        thr1.start();
        boost::this_thread::sleep(boost::posix_time::seconds(1));
    }
    return 0;
}

At which point I noticed that these threads do not destruct, they will stay in memory until program gets terminated. They also keep executing the ‘while(true)’ statement.

So I’m asking, what would cause this kind of behaviour? Is it something defined, or just a bug or something else?

  • 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-05-28T03:41:36+00:00Added an answer on May 28, 2026 at 3:41 am

    First of all interrupt will only stop the thread at certain ìnterruption points (taken from boost::threads documentation, slightly reformated):

    Predefined Interruption Points

    The following functions are interruption points, which will throw
    boost::thread_interrupted if interruption is enabled for the current
    thread, and interruption is requested for the current thread:

    boost::thread::join()
    boost::thread::timed_join()
    boost::condition_variable::wait()
    boost::condition_variable::timed_wait()
    boost::condition_variable_any::wait()
    boost::condition_variable_any::timed_wait()
    boost::thread::sleep()
    boost::this_thread::sleep()
    boost::this_thread::interruption_point()
    

    Since you don’t have any of those in your thread execution calling interrupt()on it should have no effect.

    Now for destroying the thread:

    ~thread();

    Effects: If *this has an associated thread of execution, calls detach(). Destroys *this.

    Throws: Nothing.

    The timed_join() you called on the thread should fail, since the thread won’t have finished it’s execution that fast. Therefore you didn’t join (or detach, but that wouldn’t change the ultimate outcome) your threads, meaning they do have an associated thread of execution when they are destroyed. Therefore they are detached, meaning that they will run till they are finished even through they are no longer controllable through the boost::thread object. Since they are executing and infinite loop, finishing their execution might take some time so to say.

    As a Sidenote: if you choose to change to C++11 std::threads later, you should note that destroying those without manually calling join() or detach() is not valid code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since I started learning Objective-C and Cocoa, I've been wondering why they have chosen
Since java does not support multiple inheritance I have a problem getting this to
Since I started studying object-oriented programming, I frequently read articles/blogs saying functions are better,
Since CS3 doesn't have a web service component, as previous versions had, is there
Since I started this new webshop for a friend to launch, which still in
Since in the control file I have to provide a name (hard-coded) I need
since they have same rendering engine, this problem shows in both. it works great
Since I started using Fireworks CS5.1, and it doesn't matter how I write the
Since visual studio added support for JQuery, I am wondering how to create a
Since i'm writing a game in RoR, i need to have a game loop

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.