Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ SIGABRT on new thread

I’m trying to parallelize a method inside a Parser class. Since this method requires shared mutex I wasn’t able to use OpenMP, and therefore had to go with the standard libraries.

I’m currently working with C++ 17, and here’s the main code that’s not working:

auto p = Parser(.7);
int tMax = thread::hardware_concurrency();
vector<thread> threads;

int chunk = (int)lines.size() / tMax;
for (int i = 0; i < tMax; ++i) {
       int start = chunk * i;
       threads.emplace_back(&Parser::parse, &p,lines, start, i);
}

The problem is that I get a SIGABRT at the creation of a new thread and I can’t figure out the reason for this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can find the full/messy code here. In case you want to dive into it I’ll leave a brief list of the important lines:

  • 32-110: Auxiliary objects definitions
  • 112: Start of Parser definition
  • 335: parse method definition
  • 424: main (referenced code at 443)

I looked for similar cases to mine (like this one) but I still can’t understand what is really happening and why.

Debugging only leads me to the thread constructor and into assembly code, until it reaches the terminate exception thrower.

I think I might be messing something up with address and references, since I’m passing the object itself, but I don’t have enough experience with C++ to know for certain.

I should also mention that this function works perfectly when called within the main thread, but when creating the second thread it seems to throw SIGABRT.

EDIT:
Minimal/Cleaned Example can be found here.

>Solution :

You’re creating a vector of threads and then immediately exit main without waiting for any of them to finish execution. This will cause them to crash.

Adding for(auto& t : threads) t.join(); to the end of main to wait on all the threads works in my test.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading