Sponsored Content
Top Forums Programming How can I make the parent thread wait Post 302260121 by jayfriend on Wednesday 19th of November 2008 06:19:00 PM
Old 11-19-2008
Question How can I make the parent thread wait

Hi All,

I will be glad if you could help me resolve this problem.

I have created two detachable threads and wanted to them execute independent of the parent thread ( the main task which creates the detachable threads). But I see no output coming from the execution of two detachable threads. When I debugged the application, I found that the parent thread terminates before the newly created threads complete the execution and this causes the child threads to terminate without completing their job. I have no idea what is causing the detachable threads to stop their execution. As far as I know we should be able to execute the detachable threads irrespective of the parent thread status(of course there is nothing like parent child hierarchy in posix lib by default) , please correct me if I am wrong.

Following is the sample code to reproduce the problem.

class Sample
{
public:
Sample()
{ cout <<"Sample:: constructor \n"; }
~Sample()
{ cout <<"Sample :: destructor\n"; }

};


void* ThreadFun(void * data)
{
Sample s1;
cout<<" ThreadFun "<<*((int*)(data))<<"\n";
}

int main()
{
pthread_t tid1,tid2;
pthread_attr_t attr,attr1;
int i=11;
int a =10, b=20;
cout<<" from main \n";
pthread_attr_init(&attr);
pthread_attr_init(&attr1);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
pthread_attr_setdetachstate(&attr1,PTHREAD_CREATE_DETACHED);

int j=pthread_create(&tid1,&attr,&ThreadFun,(void*)&a);

j=pthread_create(&tid2,&attr1,&ThreadFun,(void*)&b);

pthread_attr_destroy(&attr);
pthread_attr_destroy(&attr1);

//sleep(2);
return 0;
}

OUTPUT:
from main


And If I remove the comments at "sleep(2)" , then it works fine. Following is the output with sleep statement.

from main
Sample:: constructor
ThreadFun 10
Sample :: destructor
Sample:: constructor
ThreadFun 20
Sample :: destructor
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

make my script wait

is there a way to make my script wait before doing something without using the "sleep _" command? (4 Replies)
Discussion started by: Blip
4 Replies

2. UNIX for Advanced & Expert Users

how to make a parent wait on a child shells running in background?

Hi I have a shell script A which calls another 10 shell scripts which run in background. How do i make the parent script wait for the child scripts complete, or in other words, i must be able to do a grep of parent script to find out if the child scripts are still running. My Code: ... (1 Reply)
Discussion started by: albertashish
1 Replies

3. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

4. Shell Programming and Scripting

How to make parent shell finish last?

Hi all, I have a shell that kicks off several sub-shells and make them run parallelly, like: shell1.sh & shell2.sh & shell2.sh & ... However, since all sub-shells run parallely, the parent shell finished right after it's submitted, like: $ parent.sh & $ + Done parant.sh & $ ... (2 Replies)
Discussion started by: visio2000
2 Replies

5. Shell Programming and Scripting

Make cron wait for the child process

I am trying to find a list of files and writing it to a text file. Based on the machine performance the file writing will be slow at certain time. The code to find file and redirecting the output to text file is on a shell script /usr/bin/find $SEARCH_DIR -daystart \( \( -name 'KI*' -a... (4 Replies)
Discussion started by: nuthalapati
4 Replies

6. Programming

Pass parameter from a child make to a parent

Hello, I have the following problem: I have makefileproj and makefilemod in a build process for a complex project - from makefileproj I call the makefilemod. In makefilemod I generate a list containing objects eg,: "../../../25_Build/Results/Objects/FBL/Fls.o... (4 Replies)
Discussion started by: marina_lmv
4 Replies

7. Programming

Parent Thread Of Child Thread

Parent Thread Of Child Thread Suppose a process creates some threads say threadC and threadD. Later on each of these threads create new child threads say threadC1, threadC2, threadC3 etc. So a tree of threads will get created. Is there any way to find out the parent thread of one such... (1 Reply)
Discussion started by: rupeshkp728
1 Replies

8. Programming

Parent,child wait,signal

Hello. I want to make a child do some stuff,wait,then the parent does some stuff and then child does some stuff and waits again.I have made the following but it does not work.Can anybody help me? pid1 = fork(); if (pid1 == -1) { perror("Can't create child\n"); ... (18 Replies)
Discussion started by: Cuervo
18 Replies

9. Shell Programming and Scripting

parent process needs to wait

I have two scripts lets say A.expect and B.sh needs to be executed. I am executing B.sh from A.expect where B.sh has sleep command. My problem is that when B.sh encounters the sleep command my A.expect starts executing and exits. but my A.expect should execute only after completing B.sh. Is... (3 Replies)
Discussion started by: priya@2012
3 Replies

10. Shell Programming and Scripting

How make parent to wait from child process?

Hi all, I am starting mgen5 for sometime depends on input from a file, in a child process. now I want to make parent to wait in this child process till mgen5 finishes, or timeout happens. could anyone please tell me how to make parent to wait in child process in shell script? thanks... (2 Replies)
Discussion started by: girijajoshi
2 Replies
thr_min_stack(3C)					   Standard C Library Functions 					 thr_min_stack(3C)

NAME
thr_min_stack - return the minimum-allowable size for a thread's stack SYNOPSIS
cc -mt [ flag... ] file...[ library... ] #include <thread.h> size_t thr_min_stack(void); DESCRIPTION
When a thread is created with a user-supplied stack, the user must reserve enough space to run this thread. In a dynamically linked execu- tion environment, it is very hard to know what the minimum stack requirments are for a thread. The function thr_min_stack() returns the amount of space needed to execute a null thread. This is a thread that was created to execute a null procedure. A thread that does some- thing useful should have a stack size that is thr_min_stack() + <some increment>. Most users should not be creating threads with user-supplied stacks. This functionality was provided to support applications that wanted complete control over their execution environment. Typically, users should let the threads library manage stack allocation. The threads library provides default stacks which should meet the requirements of any created thread. thr_min_stack() will return the unsigned int THR_MIN_STACK, which is the minimum-allowable size for a thread's stack. In this implementation the default size for a user-thread's stack is one mega-byte. If the second argument to thr_create(3C) is NULL, then the default stack size for the newly-created thread will be used. Otherwise, you may specify a stack-size that is at least THR_MIN_STACK, yet less than the size of your machine's virtual memory. It is recommended that the default stack size be used. To determine the smallest-allowable size for a thread's stack, execute the following: /* cc thisfile.c -lthread */ #define _REENTRANT #include <thread.h> #include <stdio.h> main() { printf("thr_min_stack() returns %u ",thr_min_stack()); } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
attributes(5), standards(5) SunOS 5.10 12 May 1998 thr_min_stack(3C)
All times are GMT -4. The time now is 05:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy