tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: ucontext



On Wednesday 16 December 2009 20:53:09 Valeriy E. Ushakov wrote:
> Sad Clouds <cryintothebluesky%googlemail.com@localhost> wrote:
> > At a later time, another thread goes through the global data
> > structure, loads each thread's ucontext with setcontext() and runs
> > that thread where it left off.
> >
> > Is it safe to do the above with detached threads? When a thread
> > calls getcontext() does it get a complete copy of its context, or
> > does the context contain some pointers to the current thread's
> > data. So that if this detached thread terminates and later another
> > thread is created, the memory will be recycled and the previously
> > saved context will point to memory now owned by the new thread??
> 
> Yes.  E.g. ucontext contains sp, but it's your responsibility to have
> that stack area still valid, so that when you resume that context sp
> points to something sane.
> 
> SY, Uwe
> 

OK, let me just double check this. If I create a detached thread, save this 
thread's context and then terminated the thread, there is a possibility that 
the area of memory pointed by saved context's stack pointer may be used for 
other newly created threads, is this right?

So one way to avoid this is to create a joinable thread. There is absolutely 
nothing that will touch the stack memory, unless I call pthread_detach(), or 
pthread_join(). Is this correct?


Home | Main Index | Thread Index | Old Index