LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-24-2008, 12:38 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
pthread state


Ok, so I am writing my first "real" threaded application.

I have come to the point where I want to cancel the thread but I want to do it nicely.

So my plan was to join it, waiting for it to quit on its own. But what if the function is running an infinite loop? Is there a way I can say like "you got 5 seconds to die or I am killing you"?

Or any other ideas on how to destroy a thread in a nice way? All tutorials I've read on pthreads just say "think twice on how you cancel your threads" but none give any examples on how to do it. :P
 
Old 02-24-2008, 08:55 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938
What you should do is to signal the thread in some way that it should now please terminate itself.

For example, set a global boolean that says to the thread-world, "okay, folks, game over." Then, signal all the condition-variables that the respective threads are waiting on. They should all, in their turn, notice that the game-over flag is set, and proceed in an orderly fashion to put their houses in order and die.

If your threads naturally do things that might "run too long," then you should already have some kind of signal-mechanism in place that can be used to asynchronously notify them of the referee's-whistle. (Presumably you would use this for any sort of request. Such a signal, by itself, does not "cause the thread to die.") So you might do all three things:
  1. First, set the game-over flag.
  2. Then, signal everyone's condition-variables so if they're sleeping now they'll wake up.
  3. Finally, if necessary, send a user-signal to the threads in case they might be busy, but be sure that the threads know to ignore that signal (or at least do-nothing) when they are in the process of closing down. The signal must be "absorbed," quietly.

The strategy here is that threads handle their termination in exactly the same fashion that they handle any other sort of request: it's not handled like any sort of exception. The thread simply stops accepting new requests, releases its resources, and dies.

"Termination time" is always the second-bumpiest moment in a program's life. There's a great potential for timing-holes at that point, and your design here must be careful and thorough.

Last edited by sundialsvcs; 02-24-2008 at 08:58 PM.
 
Old 02-24-2008, 10:45 PM   #3
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
Quote:
Originally Posted by Ephracis View Post
Ok, so I am writing my first "real" threaded application.

I have come to the point where I want to cancel the thread but I want to do it nicely.

So my plan was to join it, waiting for it to quit on its own. But what if the function is running an infinite loop? Is there a way I can say like "you got 5 seconds to die or I am killing you"?

Or any other ideas on how to destroy a thread in a nice way? All tutorials I've read on pthreads just say "think twice on how you cancel your threads" but none give any examples on how to do it. :P
The thread should issue a trap command or the equivalent higher level language function call:

http://linux.die.net/man/1/trap

The parent routine should issue a kill:

kill SIGTERM pid

http://learnlinux.tsf.org.za/courses...g/ch12s03.html


--------------------------
Steve Stites
 
Old 02-25-2008, 02:21 AM   #4
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Quote:
Originally Posted by sundialsvcs View Post
For example, set a global boolean
Yeap, that's what I am doing right now. It works, but it takes a while to quit if the thread is running a sleep(10) :P

Problem here is that the whole reason I am using threads is so I can put all my third-party plugins in their own threads. So therefore I am not in control of the actual code that the thread is running.

So my solution would be to first of all provide the plugin writers with the class method
bool isRunning()
where they could check whether I want them to run or not.

However, let's say that we have a really bad developer writing a plugin which does not care about my bool, it just continues to run, or it may be inside a sleep(100) or something.

I still have control of the class that the plugin inherits. So I figured that I could use that class to send thread signals (since I do not want to make the plugins too complex to write, the developers should not have to worry about threads, they should not even know about them).

Quote:
Originally Posted by sundialsvcs View Post
"Termination time" is always the second-bumpiest moment in a program's life.
What is the bumpiest? :P

Quote:
Originally Posted by jailbait View Post
The thread should issue a trap command or the equivalent higher level language function call.

The parent routine should issue a kill.
Thanks, I will read about that and see how I can use it.
 
  


Reply



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
not work: iptables -I INPUT 5 -m state --state NEW -m tcp -p tcp --dport 3306 -j DROP abefroman Linux - Security 1 07-18-2007 08:19 AM
LXer: State by state, Microsoft responds to creeping threat LXer Syndicated Linux News 0 05-01-2007 07:16 AM
libtest.a uses pthread: user of libtest.a should not link pthread again debulu Programming 2 01-31-2007 09:23 PM
Pthread in c++ BoldKiller Programming 3 08-30-2004 05:52 AM
about pthread c12ayon Programming 6 10-25-2003 04:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:09 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration