![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
/*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); } } |
|
||||
|
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. |
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|