user438431
user438431

Reputation: 347

c++ thread assignment throwing SIGSEGV

I'm sure I'm just plain doing something stupid, but I can't seem to pinpoint it. I am trying to write a class that will become the framework for modules in our app. Each module will have a message queue and go off and process those messages in it's own thread. problem is when I call run() the thread creation and assignment to the member variable causes a SIGSEGV. How would I correctly assign the new thread to a member variable of the class?

EDIT: new test case posted

class TestModule
{
public:
    TestModule(){}
    virtual ~TestModule(){}

    void run()
    {
        // create the thread
        thrd = std::move(std::thread(&TestModule::_run, this));
    }

protected:
    std::thread thrd;                       // the running thread

private:
    void _run()
    {
        while (!true) {
            sleep(5);   // pretend to do stuff
        }
    }
};

class SimpleApp : public TestModule
{
public:
    ~SimpleApp() {}
};

int main() {
    SimpleApp app;
    app.run();

    int ctr=0;
    while (true) {
        sleep(1);
    }
    return 0;
}

Upvotes: 2

Views: 1170

Answers (2)

user438431
user438431

Reputation: 347

after moving all the code into a simple test.cpp file, I noted as did Casey, that it works just fine without seg faulting.

so I started unwinding the production code one piece at a time. It appears that a third library I was linking to (utility library that we use with all kinds of database functions and string functions, etc) was built single threaded. Once I rebuilt that library with -pthread, and re-linked to this app, the crash stopped happening.

Upvotes: 1

edmz
edmz

Reputation: 8494

std::thread will call std::terminate if you do not join() it. Check out this answer for more info. Therefore,

void TestModule::run()
{
    // what do we need to do before moving on?
    bNeedsToQuit = false;

    // create the thread
    thrd = std::move(std::thread(&TestModule::_run, this));
    thrd.join();
}

Upvotes: 0

Related Questions