DEV Community

Mukit, Ataul
Mukit, Ataul

Posted on

1

Pitfall with C++ Threads

In one of my programs, I declared a thread object accidentally inside an if condition. I completely forgot - the thread would be destroyed before properly finishing execution when the thread variable goes out of scope. It was heck of a trouble to find the core dump issue :
"terminate called without an active exception
Aborted (core dumped)"

My problematic code:

if(runnable){
    std::thread t([&app]() {
              app.run().
    });

}

Temporary Fix (I got more problems to take care off than to solve this one elegantly):

std::thread t([&app, &runnable]() {
    if(runnable){
        app.run();
    }       
            
});     

In any case, need to be very careful how and where your thread objects are declared and executed.

Signing off,

mukit

2020-07-21

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay