+ 2

what's wrong in my threadpool

Hi Getting segmentation fault on below code where I tried to implement thread pool: https://code.sololearn.com/ca18A69A181a Could anyone please help me on this?

8th Mar 2021, 12:22 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Réponses
+ 3
Looking at your commented code, it looks like you were detaching them at the time of creation only. I would say a better way to do it would be to join all the threads when your threadpool is destroyed ( in its destructor ) Like this ūüĎá https://code.sololearn.com/cuki1XIB25fG/?ref=app
8th Mar 2021, 1:47 PM
Arsenic
Arsenic - avatar
+ 3
Every thread that is launched have to to be either joined or detached. If a thread goes out of scope in joinable state then the destructor of std::thread calls std::terminate to terminate your program.
8th Mar 2021, 1:27 PM
Arsenic
Arsenic - avatar
0
Thanks ... I had actually tried with both detach and join but it was not working
8th Mar 2021, 1:31 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Yeah , working as expected... but I don't get importance to do it into destructor...I was also joining once only and current code also joins once only
9th Mar 2021, 4:13 AM
Ketan Lalcheta
Ketan Lalcheta - avatar