ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I am writing a multithreaded program using pthreads, and I'm having a problem. My program is a daemon which runs in the background and has two threads. The first thread is an infinite loop that helps with a timer function in my program. The second is a thread which uses libpcap to listen to a network interface. I want to use signal() to setup a signal handler which will properly clean up after the program, killing both threads, closing log files, etc, but I'm not sure how to do this. Currently this is what I'm doing:
Declare a global keep_going variable and set it to true
Register signal handler
Create threads 1 & 2 and wait for them to exit
Thread 1 Thread 2
while(1) pcap_loop() (essentially an infinite loop)
check for timeouts
sleep 10 seconds
The signal handler function simply closes log files, sets keep_going to 0, and calls pthread_join() on thread 1 on so that the next time the loop in thread 1 starts over, thread 1 will terminate. However, this is not ideal, because you could have to wait up to 10 seconds for thread 1 to terminate, and I don't know how to make thread 2 terminate. Does anyone have any ideas for a better way to do this?