c++ - What happens if you call exit(0) while other threads are still running? -
c++ - What happens if you call exit(0) while other threads are still running? -
suppose programme has several threads: t1, t2, etc. these using pthreads. t2 thread sitting in loop reading stream , accessing variable static storage duration.
now suppose t1 calls exit(0).
(further details: have programme doing on unix-based system, , compiled g++. programme appears crashing on shutdown stack trace indicates static variable not valid.)
does thread killed prior c++ object destruction?
is c++ not aware of threads, these maintain running until c++ cleanup complete?
should sigterm handler first shutdown or kill threads before proceeding, or happen automatically?
i'm answering question in title of question, not 3 bullet points, because think answers bullet point questions irrelevant reply actual question.
using exit when programme in random state - seem suggest - rather brutal , undeterministic way end programme single thread. doesn't matter if thread gets destroyed before object destruction or after, both ways result in nightmares. remember, each thread in random state , accessing anything. , stack objects of each thread not destroyed properly.
see documentation of exit see , doesn't clean up: http://en.cppreference.com/w/cpp/utility/program/exit
the favored way i've seen correctly shutdown multithreaded program, create sure no thread in random state. stop threads in way or another, phone call join on them feasible, , lastly remaining thread phone call exit - or return if happens in main function.
an wrong approach i've seen correctly dispose of objects, close handles, , seek proper shutdown until goes wrong, , phone call terminate. advise against that.
c++ pthreads
Comments
Post a Comment