POSIX - Hot to check if detached thread is still active


 
Thread Tools Search this Thread
Top Forums Programming POSIX - Hot to check if detached thread is still active
# 1  
Old 09-08-2008
POSIX - Hot to check if detached thread is still active

Hello,

I have created program that run threads one by one, maximum 100. Each thread will process one block of data, and once it`s finished, new thread is created with new block of data....etc

I have array of values to control status of each thread, like this:

array_thread_status[0]=1
array_thread_status[1]=0
array_thread_status[2]=0
array_thread_status[3]=1
...
array_thread_status[99]=1

When thread is created value is set to 1. When thread is finished, it set value to 0, so main program can create new thread instead.

This is all working good, so far...but I`m affraid of this scenario:

Main program
CREATE THREAD
SET VALUE TO 1


Thread
STARTING THREAD
PROBLEM - THREAD IS TERMINATED AND DIDN`T RESET VARIABLE TO 0

Main program might think that thread is still active, and new one will not be run.

So, I think that there must be another way to check weather thread is still active or not.
# 2  
Old 09-08-2008
Try pthread_join
# 3  
Old 09-08-2008
it`s detached thread
# 4  
Old 09-08-2008
How are you creating the detached threads ... please show your code.
# 5  
Old 09-08-2008
/*Mark it active*/
active_status_array[i]=1;

/*Create new threads*/
rc = pthread_create(&thdreads[i], &attr, file_parser, (void*)&thread_data_array[i]);

if (rc)
{
printf("ERROR; return code from pthread_create() is %d INDEX %d\n", rc,i);
exit(-1);
}
else
{
/*Deatach thread*/
rc = pthread_detach(thdreads[i]);
if (rc) {
printf("Got an unexpected result! rc=%d\n",rc);
exit(-1);
}

}
# 6  
Old 09-08-2008
Also, one more problem with join is that if I have 100 threads, and if they are all working and I start join wait on first one which is wait 5 min, for example. during that time other might be free...so that could slow down entire program.

My main program never ends, it`s always iteratting trought the loop and checking for free space to create new threads.
# 7  
Old 09-08-2008
I think you have a need for synchronization - meaning you do not want detached threads.

Here's why - On most systems the "thread" is really a part of the parent process - linux is an exception. This means generally there isn't a simple way to tell if a thread has terminated. If you need to know the status of a thread then you need synchronization.
Correct me where I misunderstand.

Otherwise, detached threads run whether or not the previous thread completed or not.
This is the exact opposite of synchronization.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

POSIX Thread Help

I want to create a program that creates 2 child process, and each of them creates 2 threads, and each thread prints its thread id. I0ve allread done that the outuput isn't the outuput i want. When a run the following comand "$./a.out | sort -u | wc -l" I have the folowing output 2 $: It should... (3 Replies)
Discussion started by: pharaoh
3 Replies

2. Shell Programming and Scripting

Script to check if Port is Active

is there a better way to check if a port is active on linux and sunos systems? this is currently what I'm using in my script: netstat -an | egrep -i "$PORT" i know this isn't the best way as there could be numbers in that output that has my port number in it but isn't necessarily a... (0 Replies)
Discussion started by: SkySmart
0 Replies

3. Programming

when can we destory thread attributes using pthread_attr_destroy() for detached thrd

Hi All, I am creating detached threads using pthread_create(). As we know, we need to pass the thread attribute structure as an argument to the pthread_Create() API. I want to know what is the good time to destroy this thread attributes using pthread_attr_destroy() call. Also, I want to know... (2 Replies)
Discussion started by: wonderman
2 Replies

4. Programming

detached thread is causing program crash

Hi All, I have scenario where my callback function data_update() can be called anytime. I have written the function data_update() such that it will create detached thread for processing the data sent to this function. data_update() { pthread_attr_t attr_thread; ... (1 Reply)
Discussion started by: wonderman
1 Replies

5. UNIX for Advanced & Expert Users

POSIX thread runs out of memory

i am creating threads in my program using the POSIX interface. when the thread starts executing i run out of memory and get a core dump. i have tried to increase the threads stack size using pthread_attr_setstacksize, but of no use since i guess the dynamic memory is allocated on the heap and... (1 Reply)
Discussion started by: aniketkadu2002
1 Replies

6. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

7. Programming

POSIX Thread - Memory leak

Hi all! I am implementing an http server in c++ using the posix thread, but i am having a memory leak and i cannot find the reason. I have already commented out the section that initializes the threads and i found out, the problem is when i initialize/run the threads. In the threads i have... (1 Reply)
Discussion started by: laurovalente
1 Replies

8. Programming

Posix Thread Programming

Hello, i have 2 questions: 1. Can I get the current memory usage of a thread? 2. Can I use a member-function as (void*)(*)(void*) method to create a new thread with "pthread_create(...)"?? I would be happy about any suggestion. Regards, Rolf (2 Replies)
Discussion started by: rkasel
2 Replies

9. Programming

About Native POSIX Thread Library (NPTL)

Is there anybody has documents about NPTL I wanna study about it , but can't find the documents.... anyone help appreciate :) :) :) (1 Reply)
Discussion started by: alan.zhao
1 Replies

10. Programming

Multi threading using posix thread library

hi all, can anyone tell me some good site for the mutithreading tutorials, its application, and some code examples. -sushil (2 Replies)
Discussion started by: shushilmore
2 Replies
Login or Register to Ask a Question