Question of argument list in _beginthreadex

Question of argument list in _beginthreadex

Post by Allen S » Mon, 30 Sep 2002 05:26:19



If calling "_beginthreadex(NULL,NULL,&MyThread,&MyStruct,NULL,&tID);" to
pass a struct of data, "MyStruct", into the newly created thread--MyThread,
and if the parent thread may terminate earlier, is that true that the piece
of data, "MyStruct", may get lost in "MyThread"?  Since "MyStruct" is passed
into "MyThread" by reference.

To keep "MyStruct" in "MyThread" in case that its parent terminates,
"MyStruct" must be duplicated before the parent thread terminates.  Am I
right on this?

Thanks a lot!

 
 
 

Question of argument list in _beginthreadex

Post by Scott McPhillip » Mon, 30 Sep 2002 06:49:42



> If calling "_beginthreadex(NULL,NULL,&MyThread,&MyStruct,NULL,&tID);" to
> pass a struct of data, "MyStruct", into the newly created thread--MyThread,
> and if the parent thread may terminate earlier, is that true that the piece
> of data, "MyStruct", may get lost in "MyThread"?  Since "MyStruct" is passed
> into "MyThread" by reference.

> To keep "MyStruct" in "MyThread" in case that its parent terminates,
> "MyStruct" must be duplicated before the parent thread terminates.  Am I
> right on this?

> Thanks a lot!

That depends on how MyStruct was allocated.  It will not be lost if it
is in the heap.

--
Scott McPhillips [VC++ MVP]

 
 
 

Question of argument list in _beginthreadex

Post by Allen S » Mon, 30 Sep 2002 08:19:11



> That depends on how MyStruct was allocated.  It will not be lost if it
> is in the heap.

> --
> Scott McPhillips [VC++ MVP]

Even if MyStruct is allocated in the heap, if it will be re-used later in
the parent thread, it's still better for MyThread to duplicate a copy of
MyStruct, since we don't want to the change of MyStruct in the parent change
it in MyThread.  Am I right about this?

Thanks a lot!

 
 
 

Question of argument list in _beginthreadex

Post by Scott McPhillip » Mon, 30 Sep 2002 08:50:02




> > That depends on how MyStruct was allocated.  It will not be lost if it
> > is in the heap.

> > --
> > Scott McPhillips [VC++ MVP]

> Even if MyStruct is allocated in the heap, if it will be re-used later in
> the parent thread, it's still better for MyThread to duplicate a copy of
> MyStruct, since we don't want to the change of MyStruct in the parent change
> it in MyThread.  Am I right about this?

> Thanks a lot!

Well yes, but for the thread to duplicate it safely it must be protected
with a critical section.  It is better to create a copy in the parent
thread and pass it to the new thread when creating it, then the parent
thread should never touch that one again.

--
Scott McPhillips [VC++ MVP]