The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
POSIX Thread - Memory leak laurovalente High Level Programming 1 05-08-2008 12:26 PM
Solaris DiskSuite, boot from detached disk carlossg SUN Solaris 3 12-21-2007 12:30 AM
Posix Thread Programming rkasel High Level Programming 2 09-29-2005 09:54 AM
About Native POSIX Thread Library (NPTL) alan.zhao High Level Programming 1 05-13-2005 06:14 AM
Multi threading using posix thread library shushilmore High Level Programming 2 09-09-2002 07:12 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-08-2008
orangem orangem is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 5
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 (permalink)  
Old 09-08-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Try pthread_join
  #3 (permalink)  
Old 09-08-2008
orangem orangem is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 5
it`s detached thread
  #4 (permalink)  
Old 09-08-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
How are you creating the detached threads ... please show your code.
  #5 (permalink)  
Old 09-08-2008
orangem orangem is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 5
/*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 (permalink)  
Old 09-08-2008
orangem orangem is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 5
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 (permalink)  
Old 09-08-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0