LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: 0
Terminating one thread from another


Hi,
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
--
Initialization
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)
if(!keep_going)
pthread_exit()
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?
Thanks,
Ben
 
Old 05-19-2004, 10:33 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
http://www.rt.com/man/sigwait.3.html

i'd try using those functions
 
Old 05-20-2004, 07:25 PM   #3
orinjus
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.
 
  


Reply


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 12:07 PM
X display terminating SUSE 9.2 techlife Linux - Software 5 04-01-2005 11:41 AM
Terminating an unresponsive program dangerousdave Linux - Newbie 5 02-16-2005 04:48 PM
programs terminating santasballz Linux - Newbie 3 03-10-2004 09:47 AM
terminating wvdial Freestone Linux - Newbie 6 02-22-2003 12:45 PM


All times are GMT -5. The time now is 03:12 AM.

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