Reply to comment

Don't Join on Yourself!

My apparent memory leak from the other day did in fact turn out to be leaking thread resources. The key: Don't Join on Yourself!

If a thread attempts to call pthread_join on itself it is likely to cause a deadlock in that thread that you may never know about. Particularly if you are using some sort of worker-thread model that creates and destroys threads often.

A simple way to catch this problem would be to add an assertion in your destructor.

class threaded_class
{
    ~threaded_class()
    {
        // if somehow this thread tries to join on itself, perhaps by causing this
        // instance of threaded_class to get destroyed, cause an assertion failure
        // so we can investigate it
        assert(!pthread_equal(m_worker_thread, pthread_self()));
        pthread_join(m_worker_pthread);
    }
 
    pthread_t m_worker_thread;
};

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post PHP code. You should include <?php ?> tags.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.