Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-19-2004, 09:17 PM   #1
LQ Newbie
Registered: Dec 2002
Distribution: Debian Sid
Posts: 13

Rep: Reputation: 0
Terminating one thread from another

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:

Main thread
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?
Old 05-19-2004, 11:33 PM   #2
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30

i'd try using those functions
Old 05-20-2004, 08:25 PM   #3
LQ Newbie
Registered: Dec 2002
Distribution: Debian Sid
Posts: 13

Original Poster
Rep: Reputation: 0
Excellent, thanks a bunch. The pthread_kill() function was just what I needed.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
pppd keeps terminating connection gkiagia Linux - Networking 9 06-15-2005 01:07 PM
X display terminating SUSE 9.2 techlife Linux - Software 5 04-01-2005 12:41 PM
Terminating an unresponsive program dangerousdave Linux - Newbie 5 02-16-2005 05:48 PM
programs terminating santasballz Linux - Newbie 3 03-10-2004 10:47 AM
terminating wvdial Freestone Linux - Newbie 6 02-22-2003 01:45 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration