about thread inside another thread

about thread inside another thread

Post by subbukamat » Mon, 31 Oct 2005 13:33:13


hi ,

i am new to threads programming. here i have situation where i
have to create a thread inside another thread.is it possible to create
a thread inside another thread ?..i searched in net. but i could not
find..



my first thread :

pthread_create( &callThd[thno], &attr, jobSchedule, (void *)thno);

inside jobSchedule i need to create a another thread which should call
execute2..




pthread_create( &callThd[newthno], &attr, execute2, (void *)newthno);


plz help me


thank you
 
 
 

about thread inside another thread

Post by Ulrich Eck » Mon, 31 Oct 2005 18:32:02


When your program starts (i.e. when it enters main()), you already have a
thread running, so in other words you always need a thread to start
another thread (without a thread, there wouldn't be anything happening).

Other than that, whether you can start other threads from any thread other
than the main thread depends on the threading library you use, but seeing
that you use the POSIX threading API, that works.

If you have any problems with that, you should rather tell us what you do,
what happens and what you expect to happen, alongside with a minimal piece
of code that demonstrates your problem.

Uli

--
Questions ?
see C++-FAQ Lite: http://www.yqcomputer.com/ ++-faq-lite/ first !

 
 
 

about thread inside another thread

Post by subbukamat » Tue, 01 Nov 2005 03:10:25

Hi all,

i am working on scheduler.. My main program creates thread1.


pthread_create( &callThd[thno], &attr, jobExecute, (void *)thno);

that means its running the contents in jobexecute.. Inside jobExecute i
am trying to create another thread.which should run execute function.

pthread_create( &callThd[newthno], &attr,execute, (void
*)newthno);


but its not running the execute function, here
with i attached the code.




void main()
{

pthread_attr_t attr;
pthread_mutex_init(&mutexsum, NULL);

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);


system("clear");
//thno++;
pthread_create( &callThd[thno], &attr, jobSchedule, (void *)thno);

thno++;
pthread_create( &callThd[thno], &attr, jobExecute, (void *)thno);


pthread_attr_destroy(&attr);
/* Wait on the other threads */

for(int i=0;i<=thno;i++)
{
pthread_join( callThd[thno], (void **)&status);
}


pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);

}






void *jobExecute(void *arg)
{
..
....
...

pthread_create( &callThd[newthno], &attr, execute, (void *)newthno);
...
}


void *execute(void *arg)
{
cout<<"inside thread's thread";
}


...


i am using POSIX threading API on sunOS 5.9.


please help me .


raja
 
 
 

about thread inside another thread

Post by Ulrich Eck » Tue, 01 Nov 2005 03:34:29


You should read the FAQ at parashift.com about that.

[snipped code]

There are several problems with the code. First one, it's not complete.
Second one, it seems to use several global variables. Third one is that
it's not a _minimal_ example that demonstrates your problem.

First thing you should do is think about who owns what data and, if it is
shared and concurrent accesses take place how those are synchronized. For
your info, pthread_create takes a function pointer and a data pointer. The
function pointer is where the new thread enters existence, the data
pointer is to provide the new thread with some context, i.e. so you don't
have to store data in global variables.

Other than that, you should not have to use any cast except the one
static_cast<> inside the thread function to convert the typeless pointer
back to what it initially was.

Lastly, you don't check for a single error in your code. How do you expect
it to behave properly if you don't even do that? Just a hint: in C++, you
can separate error-handling code from the 'real' code by using exceptions.

BTW, how do you determine that execute() is not called? Remember that
std::cout is buffered and that you don't flush that buffer anywhere,
neither implicit (via std::endl) nor explicitly.

Uli

--
Questions ?
see C++-FAQ Lite: http://www.yqcomputer.com/ ++-faq-lite/ first !