LinuxQuestions.org
Visit Jeremy's Blog.
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 08-29-2005, 08:47 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
restart thread...


Hi i have a small programme that has 4 threads... I want to restart a thread.... which means stop what u are doing (i dont know if this is the kill) and then start again working again.. Do u know if i can do that using signals or i must use something else for that?
 
Old 08-29-2005, 03:15 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
You can use pthread_cancel to stop the thread. Hard to say if that's the solution you want, but it may be. You can then start a new thread.
 
Old 08-29-2005, 05:45 PM   #3
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
and how i can start a new thread?
pthread_create call?
 
Old 08-30-2005, 04:54 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Yes. In your case, after the old one is correctly closed, temprary variables cleared etc.
 
Old 08-31-2005, 09:02 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
I don't recommend "killing" a thread, in the sense of taking a gun to its head and pulling the trigger. There is, as in real life, a lot of messy stuff to clean up when you do that...

Instead, you should somehow signal the thread that whatever it's working on is no longer important and the work should be discontinued. Then, allow the thread to continue living, so that it can wake up, take the next unit of work off of some queue, and begin to work on that instead.

If you want the thread to suspend what it is doing, once again you signal it in some way, telling it to wait on some event. When the time comes, trip the event. In all cases the relationship between master and child should be as asynchronous as possible.
 
Old 08-31-2005, 09:36 AM   #6
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
I agree with u but do u have any idea or advice of how i can implement that?
 
Old 08-31-2005, 02:34 PM   #7
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
You can use a find of variable with atomic operations. Their types start from atomic_
 
Old 09-01-2005, 02:26 AM   #8
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
"a find of variable" What does your statement means?
 
Old 09-01-2005, 08:58 AM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Start with man pthreads and look over what's available. Threads, since they all live in one address space, can communicate with each other through boolean flags, e.g. for things like "please stop doing what you're doing now." To coordinate their activities, they use mutexes (mutual-exclusions) and condition-variables.

As a general, high-level overview: first of all, there should not be a one-to-one correspondence between 'a request' and 'a thread.' Don't take the "flaming arrow" approach, wherein each time a new request comes in you light up another flaming arrow (a new thread...) and shoot it into the air. Instead, place the new request onto a queue, using a mutex to protect it, then signal a condition-variable that a certain, controllable number of worker-threads are waiting on. One of the threads will wake up, dequeue the request, execute it, and place it on a completed-queue for disposal. Usually, one master-thread has the chore of accepting new work, placing it on the to-do list, and harvesting the completed work from the completed-queue.

The advantage of this approach is that (what IBM calls) the "multiprogramming level" (MPL) is always controlled; always predictable. This prevents thrashing. The MPL is set according to what the system can reasonably withstand, and if the instantaneous workload exceeds that, some work piles-up in the queue.

You can learn a lot about this sort of thing by working in, or watching, a well-run McDonald's restauraunt. Pick a shop that's close to a factory, walk in about noon, buy a greasy burger (what t'heck), and watch a well-designed workload management system operating at overload. Watch how the front-line servers manage the workload, "parking" orders and taking the next one. Watch how the manager responds, and how the back-line employees do their work. The queues may be piling up but the system is not breaking down.

(Unless... the cash-registers break down... McD's employees can't do simple math.)
 
Old 09-01-2005, 09:22 AM   #10
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
I agree with u but i dont want this..
I have 2 threads
the calculates some parameteres and then passes them to the second thread to reconfigure the server
Lets say for some reason the second thread(reconfigure) cant proceed f.e timeouts.. network bottlenecks etc Then it is possible that the first thread has new parameters for the second thread...Thats why i want to restart second thread.. I dont care anymore about the previous values but i want the second thread to run again with the latest values...
Is it clear now? Thx a lot
 
Old 09-01-2005, 02:41 PM   #11
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
Originally posted by alaios
"a find of variable" What does your statement means?
Spelling mistake. Should be: kind of. Atomic variable created before threads (so they all have access to it) can be increased by one thread. Second can read it from time to time and so something if the value is more than ... (for example: more than zero).
 
  


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
Test thread (Ignore this thread) operatester General 2 07-07-2005 02:04 PM
[thread control suggestion] add a "solved" button that the thread starter can click atom LQ Suggestions & Feedback 3 03-24-2005 11:55 AM
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 09:15 AM
can a reader thread and a writer thread r/w to same fd mxandres Programming 1 07-18-2004 10:56 PM
configure qt thread issue (just compiled qt w/ -thread option) cleff Linux - Software 8 05-07-2004 11:11 PM

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

All times are GMT -5. The time now is 10:44 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