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:
- 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).
- your program has some bugs that triggers e.g a SIGSEGV,
- your program receives an extern signal that it does not catch and whose default action is to terminate the process.
- ...
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
)
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