thread argument problem

  • Follow


I have problem with pass structure to thread function in SOLARIS.When I
pass it to created thread it always has empty values.

my structure defined as:
typedef struct momMSG{
	struct mbhdr hd;
	struct SCRIPTMSG MOMBuf;
}MY_SCRIPTMSG;

In my main function:

MY_SCRIPTMSG rcvVLMOMbuf;
--- fill rcvVLMOMbuf with values
if((rv = thr_create(NULL,0,ServerThread,(void *)
&rcvVLMOMbuf,THR_DETACHED, &tid)) == 0)

And in thread  function:
void* ServerThread(void *arg)
{
    MY_SCRIPTMSG* preceivedMSG =  (MY_SCRIPTMSG*) arg;
}
The pointer preceivedMSG  point on empty values.
What is the reason thanks

0
Reply is_vlb50 (6) 2/28/2006 9:11:56 AM

is_vlb50@hotmail.com wrote:
> I have problem with pass structure to thread function in SOLARIS.When I
> pass it to created thread it always has empty values.
>
> my structure defined as:
> typedef struct momMSG{
> 	struct mbhdr hd;
> 	struct SCRIPTMSG MOMBuf;
> }MY_SCRIPTMSG;
>
> In my main function:
>
> MY_SCRIPTMSG rcvVLMOMbuf;
> --- fill rcvVLMOMbuf with values
> if((rv = thr_create(NULL,0,ServerThread,(void *)
> &rcvVLMOMbuf,THR_DETACHED, &tid)) == 0)
>
> And in thread  function:
> void* ServerThread(void *arg)
> {
>     MY_SCRIPTMSG* preceivedMSG =  (MY_SCRIPTMSG*) arg;
> }
> The pointer preceivedMSG  point on empty values.
> What is the reason thanks

A likely reason may be that you pass a pointer to a structure object on
the stack. By the time your new thread starts executing the object may
well be gone and you are left with a stale pointer. To fix allocate the
structure object on the heap using malloc() and free() it in the new
thread.

0
Reply Maxim 2/28/2006 9:49:24 AM


You mean that I need before  thr_create clone my structure MY_SCRIPTMSG
to another,allocated by malloc() ?
Thanks

0
Reply is_vlb50 2/28/2006 10:01:24 AM

In article <1141120884.024803.20560@j33g2000cwa.googlegroups.com>,
 is_vlb50@hotmail.com wrote:

> You mean that I need before  thr_create clone my structure MY_SCRIPTMSG
> to another,allocated by malloc() ?
> Thanks

Yes.

P.S. Please include context so your question makes sense.  See

http://cfaj.freeshell.org/google/

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
0
Reply Barry 2/28/2006 10:32:20 AM

is_vlb50@hotmail.com writes:

> if((rv = thr_create(NULL,0,ServerThread,(void *)&rcvVLMOMbuf,THR_DETACHED, &tid)) == 0)

Unless you explicitly want your software to be non-portable, there
is no justification for using Solaris threads in new code. You
should be using POSIX threads instead.

Cheers,
-- 
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
0
Reply Paul 3/1/2006 5:01:23 AM

4 Replies
163 Views

(page loaded in 0.093 seconds)

Similiar Articles:













7/26/2012 6:14:56 PM


Reply: