0

I'm using Boost::thread to implement an InterruptibleThread class, while getting segmentation fault during execution. Any idea?

The source and the output are under below.

interruptiblethread.h

#ifndef INTERRUPTIBLETHREAD_H
#define INTERRUPTIBLETHREAD_H

#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
#include <boost/thread/tss.hpp>

class InterruptFlag
{
public:
    inline void set()
    {
        boost::lock_guard<boost::mutex> guard(_mtx);
        _set = true;
    }

    inline bool is_set()
    {
        std::cout << "is_set()" << std::endl;
        boost::lock_guard<boost::mutex> guard(_mtx);
        std::cout << "is_set() end" << std::endl;       
        return _set;
    }

private:
    boost::mutex _mtx;
    bool _set;
};

extern boost::thread_specific_ptr<InterruptFlag> this_thread_interrupt_flag;

class InterruptibleThread
{
public:
    template<typename FunctionType>
    InterruptibleThread(FunctionType f)
    {
        boost::promise<InterruptFlag*> p;
        _internal_thread = boost::thread([f, &p]()
        {
            p.set_value(this_thread_interrupt_flag.get());
            f();
        });
        _interrupt_flag = p.get_future().get();
    }

    inline void interrupt()
    {
        if (_interrupt_flag != nullptr)
        {
            _interrupt_flag->set();
        }
    }

private:
    boost::thread _internal_thread;
    InterruptFlag* _interrupt_flag;
};

#endif // INTERRUPTIBLETHREAD_H

interruptiblethread.cpp

#include <iostream>
#include <functional>

#include "interruptiblethread.h"

using std::cout; using std::endl;
using std::function;

boost::thread_specific_ptr<InterruptFlag> this_thread_interrupt_flag;

struct thread_interrupted {};

void interruption_point()
{
    cout << "interruption_point()" << endl;
    if (this_thread_interrupt_flag->is_set())
    {
        cout << "is_set" << endl;       
        throw thread_interrupted();
    }
}

void foo()
{
    while (true)
    {
        cout << "iterate" << endl;
        try
        {
            interruption_point();
        } catch (const thread_interrupted& interrupt)
        {
            cout << "catch thread_interrupted" << endl;
            break;
        }
    }
}

int main()
{
    InterruptibleThread int_thread(foo);
    int_thread.interrupt();
    while (true) {}
}

Output:

➜  build ./main
iterate
interruption_point()
is_set()
[1]    44435 segmentation fault  ./main
Qi W.
  • 706
  • 9
  • 21
  • 1
    `this_thread_interrupt_flag` is never "initialized" - it still holds a nullptr? – Simon Kraemer Feb 23 '16 at 15:08
  • Thanks man. I think it has been initialized, 'cause it has output "is_set()". Though I'm not sure about this. – Qi W. Feb 23 '16 at 15:23
  • I don't think so. I think it isn't initialized and your call to `is_set` is UB. Please check this question: http://stackoverflow.com/questions/6021273/how-to-allocate-thread-local-storage – Simon Kraemer Feb 23 '16 at 15:25
  • Yeah, it works! It does need to initialize `this_thread_interrupt_flag`. Thank you indeed! – Qi W. Feb 23 '16 at 15:39

1 Answers1

1

this_thread_interrupt_flag is not initialized. Please initialize it correctly as described here

Your call to is_set is UB.

Community
  • 1
  • 1
Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49