troubleshooting Question

inconsistent behavior killing threads c++/Linux

Avatar of mitchguy
mitchguy asked on
3 Comments1 Solution338 ViewsLast Modified:
The application I'm working on (c++/Linux) has
five threads all together.
Each of them have a while loop where instead of putting
while(1) and killing my program with a ctrl + C key combination, I wanted to terminate my program by catching the ctrl + C with a signal and setting a shared variable.
//this is in the top of the function of the first thread
struct sigaction act;
act.sa_handler = &(shutdown);
act.sa_flags = 0;
sigaction(SIGINT, &act,0);
void shutdown(int sig){
quit = true;
all of the while loops are now while(!quit)

I have created this class which is going to be merged with
other peoples code so I dont have access to the target main() only mine which is for testing.
all of my thread functions are member fuctions of my class.

as it stands right now sometimes my app shuts down after
a ctrl-c sometimes it doesn't, sometimes it does only after about 30 seconds has past.

so I guess what my question boils down to is, should I
have a handler declared in each function with it's own
kill function associated with it or can they all share one. If they can share one where should it go?
I arbitrarily picked the first thread function launched.


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros