Free the stack for the thread

Hi there,

I have used beginthread() in my driver to create a thread and I allocate the
stack in my program instead of letting the system to allocate the stack. The
reason is that I want to be able to free the stack evertime the thread end
so I had the program like the folllowing:

main()
{
char *args[2];

args[0]=(char *)malloc(STACK_SIZE);
_beginthrad(child,args[0],STACK_SIZE,args);


}

child(void far *parm)
{
char * stack =parm[0];


free(stack);
_endthread();
}

So my question is that can I free the stack before calling _endthared()? I
have run the program for some stress test, and so far, it doesn’t seem has
any problem, but I am still wondering is there any good suggestion for the
stack free up.

Thanks in advance!
Teresa

This is potentially dangerous.

First, free and malloc are NOT threads safe…

Second you can’t free the stack within the process. It works
most of the time because of the way memory is allocated
in QNX4. But this potentially dangerous.

Instead in your main() add some intellegence to
let thread reuse stack space (so you never
free it per se)

“Teresa Tao” <ttao@neontech.com> wrote in message
news:8sg80h$6ak$1@inn.qnx.com

Hi there,

I have used beginthread() in my driver to create a thread and I allocate
the
stack in my program instead of letting the system to allocate the stack.
The
reason is that I want to be able to free the stack evertime the thread end
so I had the program like the folllowing:

main()
{
char *args[2];

args[0]=(char *)malloc(STACK_SIZE);
_beginthrad(child,args[0],STACK_SIZE,args);
.
.
}

child(void far *parm)
{
char * stack =parm[0];
.
.
free(stack);
_endthread();
}

So my question is that can I free the stack before calling _endthared()? I
have run the program for some stress test, and so far, it doesn’t seem has
any problem, but I am still wondering is there any good suggestion for the
stack free up.

Thanks in advance!
Teresa
\

Greetings,

First, free and malloc are NOT threads safe…

Is this in QNX or Neutrino (do you notice that this question is being asked
a great deal recently). Anyway, according to the Neutrino Library Reference
free and malloc are most certainly thread safe.

Perhaps I misunderstand something.

]{

Kristoph A. Cichocki-Romanov <news1@kristoph.net> wrote:
: Greetings,

:> First, free and malloc are NOT threads safe…

: Is this in QNX or Neutrino (do you notice that this question is being asked
: a great deal recently). Anyway, according to the Neutrino Library Reference
: free and malloc are most certainly thread safe.

: Perhaps I misunderstand something.

It’s QNX 4 – there’s no _beginthread() under QNX Neutrino.


Steve Reid stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems Ltd.

In article <8sg80h$6ak$1@inn.qnx.com>, Teresa Tao <ttao@neontech.com> wrote:

Hi there,

I have used beginthread() in my driver to create a thread and I allocate the
stack in my program instead of letting the system to allocate the stack. The
reason is that I want to be able to free the stack evertime the thread end
so I had the program like the folllowing:

main()
{
char *args[2];

args[0]=(char *)malloc(STACK_SIZE);
_beginthrad(child,args[0],STACK_SIZE,args);
.
.
}

child(void far *parm)
{
char * stack =parm[0];
.
.
free(stack);
_endthread();
}

So my question is that can I free the stack before calling _endthared()? I
have run the program for some stress test, and so far, it doesn’t seem has
any problem, but I am still wondering is there any good suggestion for the
stack free up.

No, you cannot free the stack before _endthread(). It is certain to
cause a segmentation violation at some point. Since a thread in
QNX 4 is really another process that shares the same memory space,
you can write a “reaper” – a SIGCHLD signal handler – that performs a
wait() to get the exit status of threads. If you keep track of the
stack mappings for every thread you create in a global data structure,
the reaper can free it after the wait() indicates that the process has
exited.

Thanks in advance!
Teresa

\

Steve Furr email: furr@qnx.com
QNX Software Systems, Ltd.