Sponsored Content
Full Discussion: Infinite thread
Top Forums Programming Infinite thread Post 302482641 by Praveen_218 on Wednesday 22nd of December 2010 06:50:43 AM
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..
 

5 More Discussions You Might Find Interesting

1. 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

2. 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

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. 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

5. 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
PTHREAD_JOIN(3) 					     Library Functions Manual						   PTHREAD_JOIN(3)

NAME
pthread_join - wait for termination of another thread SYNOPSIS
#include <pthread.h> int pthread_join(pthread_t th, void **thread_return); DESCRIPTION
pthread_join suspends the execution of the calling thread until the thread identified by th terminates, either by calling pthread_exit(3) or by being cancelled. If thread_return is not NULL, the return value of th is stored in the location pointed to by thread_return. The return value of th is either the argument it gave to pthread_exit(3), or PTHREAD_CANCELED if th was cancelled. The joined thread th must be in the joinable state: it must not have been detached using pthread_detach(3) or the PTHREAD_CREATE_DETACHED attribute to pthread_create(3). When a joinable thread terminates, its memory resources (thread descriptor and stack) are not deallocated until another thread performs pthread_join on it. Therefore, pthread_join must be called once for each joinable thread created to avoid memory leaks. At most one thread can wait for the termination of a given thread. Calling pthread_join on a thread th on which another thread is already waiting for termination returns an error. CANCELLATION
pthread_join is a cancellation point. If a thread is canceled while suspended in pthread_join, the thread execution resumes immediately and the cancellation is executed without waiting for the th thread to terminate. If cancellation occurs during pthread_join, the th thread remains not joined. RETURN VALUE
On success, the return value of th is stored in the location pointed to by thread_return, and 0 is returned. On error, a non-zero error code is returned. ERRORS
ESRCH No thread could be found corresponding to that specified by th. EINVAL The th thread has been detached. EINVAL Another thread is already waiting on termination of th. EDEADLK The th argument refers to the calling thread. AUTHOR
Xavier Leroy <Xavier.Leroy@inria.fr> SEE ALSO
pthread_exit(3), pthread_detach(3), pthread_create(3), pthread_attr_setdetachstate(3), pthread_cleanup_push(3), pthread_key_create(3). LinuxThreads PTHREAD_JOIN(3)
All times are GMT -4. The time now is 01:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy