Infinite thread


 
Thread Tools Search this Thread
Top Forums Programming Infinite thread
# 1  
Old 12-22-2010
Infinite thread

I created a thread which pings a machine for every 15 seconds. I made my thread function in infinite loop. Main process also in infinite loop and will run for years. I wonder the thread will continue as long as main process continuous or will thread terminates at some point? Is there any life period for the thread created, any performance degrade because of infinite thread ?

Code:
void PingFunc()
{
  while (1)
  {
  		PingMachine();		
		sleep(15);
   }
}

main()
{
 pthread_t thId;
 pthread_create (&thId, NULL, PingFunc,NULL);
 while(1)
 {
     getrequest();
     processrequest();  
  }
}

# 2  
Old 12-22-2010
Under normal & trivial circumstances, in your case, your pthread is never going to die, because the execution in context to this thread doesn't reaches past the ending curly braces }.

Also main is a non-terminating again.

Here are the general cases, a pthread dies:

Code:
1) When there is an explicit call to pthread_exit()

2) When the execution reaches the end curly braces; i.e. the handler function has been executed and/or returns.

3) When the main() terminates; results in the clean up of all the program stack and memory recovery by the OS; lets get the pthread stack cleaned up.

Now coming to few more general cases pertaining to your present code:

Code:
1) You haven't handled any signal/interrupts; so don't expect it to run for years. In other words, its a no robust code. Hence, the moment main()
 is gone, your thread would go.

2) main() is in the busy wait; most of the OS kernel scheduler would detect this easily and (untill explictly elevated in terms of priority) would run off with a low priority in scheduling 
--happens in Linux (trick to overcome in busy wait is to put a few miliseconds of sleep  --but can't generalize this for other OS). 
Instead why don't you just blocked wait in pthread_join() and do that function call in another seperate pthread?


Last edited by Praveen_218; 12-22-2010 at 10:10 AM..
# 3  
Old 12-22-2010
Thank You. I have a signal handler and getrequest is a blocked wait and it acts when message received.
I wonder as long as main goes the thread will go, even for months. In main I can create a new thread for every 15 secs to ping the machine and detach the thread. Instead of creating for every 15 seconds , I had it in one thread.
# 4  
Old 12-22-2010
Good evening,

Quote:
Originally Posted by satish@123
I created a thread which pings a machine for every 15 seconds. I made my thread function in infinite loop. Main process also in infinite loop and will run for years. I wonder the thread will continue as long as main process continuous or will thread terminates at some point? Is there any life period for the thread created, any performance degrade because of infinite thread ?
Theoretically, there is no any life time period imposed on a created thread. Practically, the performance may degrade over the time or your process terminates because of programming mistakes, like:
  1. your thread has a memory leak, a consume more and more heap or stack. This will cause performance degradation over the time (e.g. because swap is then used), or even termination (e.g. on Linux, the infamous oom killer kills your process).
  2. your program has some bugs that triggers e.g a SIGSEGV,
  3. your program receives an extern signal that it does not catch and whose default action is to terminate the process.
  4. ...
But if your program is rock solid, then it will go on and on. Until the hardware where it runs breaks (and like death, you know this will happen at some point Smilie )

Quote:
Originally Posted by Praveen_218
1) You haven't handled any signal/interrupts; so don't expect it to run for years. In other words, its a no robust code. Hence, the moment main() is gone, your thread would go.
This is slightly incorrect. A uncaught signal won't necessarily cause main() to go away. What happens actually, is that the signal is delivered to a particular thread in the process. If the signal is not blocked for this thread, then the signal handler installed shall run in the context of the thread that "received" the signal. If no handler is installed, then the default action shall take place. This default action may cause the process to terminate.

Quote:
Originally Posted by Praveen_218
2) main() is in the busy wait; most of the OS kernel scheduler would detect this easily and (untill explictly elevated in terms of priority) would run off with a low priority in scheduling
--happens in Linux (trick to overcome in busy wait is to put a few miliseconds of sleep --but can't generalize this for other OS).
Instead why don't you just blocked wait in pthread_join() and do that function call in another seperate pthread?
How can you say that without knowing what getrequest()/processrequest() is doing? getrequest() may for instance involve some blocking syscall...

Quote:
Originally Posted by satish@123
In main I can create a new thread for every 15 secs to ping the machine and detach the thread. Instead of creating for every 15 seconds , I had it in one thread.
This is IMHO better to have one thread that runs for ever, as shown in your snippet.

HTH, Loïc
This User Gave Thanks to Loic Domaigne For This Post:
# 5  
Old 12-23-2010
Quote:
Originally Posted by Loic Domaigne
Good evening,

This is slightly incorrect. A uncaught signal won't necessarily cause main() to go away. What happens actually, is that the signal is delivered to a particular thread in the process.

HTH, Loïc
Which thread it is delivered, if its not main() ? Request you to kindly explain in details.
# 6  
Old 12-26-2010
Quote:
Originally Posted by Praveen_218
Which thread it is delivered, if its not main() ? Request you to kindly explain in details.
Accordingly to Bill Gallmeister's corollary, the signal is delivered to the most evil possible thread Smilie

Cheers, Loïc
# 7  
Old 01-04-2011
Infinite thread

An infinite loop is dangerous to a program (especially on older computers). If it is not stopped at least at some point it will or at least can potentially crash the host computer.

Maybe include some information as to what you are attempting to build or what the function does and we can help you out more.

For Loops and Do While loops are probably best for what you are looking to accomplish.
____________________
fun | mobile | free download | world call
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to stop infinite loop

Im unable to stop the below infinite loop (bash script). Can someone tell me why this isnt responding to signals eg: ctrl+c (SIGINT) or ctrl+z c=0 test_loop() { c=$(($c+1)) echo "count value is : $c " sleep 1 test_loop } Im using: SunOS 5.10 PS: If run this as... (13 Replies)
Discussion started by: Arun_Linux
13 Replies

2. Homework & Coursework Questions

Help with infinite loop problem

1. The problem statement, all variables and given/known data: My problem is an infinite loop when i press any other key other then Y or y in the while loop. what i want it to do is return to the normal script outside of it if pressing N or n or keep asking the same question if its any other... (4 Replies)
Discussion started by: Ren_kun
4 Replies

3. Shell Programming and Scripting

Infinite while loop

what is the difference between while:,while true and while false? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

4. Programming

high priority thread contains an infinite loop

Hi, Assume there are 3 threads N1, N2, N3. in N1 { .... while(1) { } } when the thread N1 got the time slice, it started executing the infinite loop.. Note:there is no condition inside the while(1) to end the infinite loop I heard that some RTOS will solve this problem... (0 Replies)
Discussion started by: rvan
0 Replies

5. Programming

How to cancel a thread safely from the initial thread?

how about asynchronous canceling? or with signal? if with signal whether it effects the process? my english so badly :( :( (1 Reply)
Discussion started by: alan.zhao
1 Replies
Login or Register to Ask a Question