This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: how to wait for a thread to complete?


Ãyvind Harboe <oyvind.harboe@zylin.com> writes:

> I've tried and failed to find a reasonable way to wait for
> a thread to run to completion in eCos. By "run to completion",
> I mean that the thread might yet do useful work(including e.g.
> network communication, processing, etc.).
> 
> cyg_thread_delete() will break a thread out of its sleep, which 
> I don't want.
> 
> Setting a flag just before I return from the thread entry fn is not
> satisfactory, because it could cause me to reuse the stack
> prematurely and a subsequent cyg_thread_delete() *could* fail.
> 
> So, I propose that a new API fn in eCos is called for. Something along
> the lines in the attached patch.

This patch also suffers from the same problem. What happens if the
memory for the thread object has been reused before the call to
cyg_thread_wait_exited()? Any system that might cause you to reuse the
stack prematurely would cause you to reuse the thread object
prematurely too.

Also, the function may never terminate if the target thread never gets
to run and put itself into EXITED state. You may need to do more than
just wait to get the thread to exit -- like boost its priority for
example.


> With regards to the implementation, I would have liked for the
> cyg_thread_delay() to be replaced by something that caused
> cyg_thread_wait_exited() to wake up *immediately* after the thread has
> entered the exited state, but I couldn't find an obvious and simple way
> to do so.

Any mechanism to do this in a generic manner is rather complicated.
More complicated that we wanted to build into the basic kernel or
API. The intention behind the kernel API was that it should be
minimal, offloading the more sophisticated things to higher levels, or
to the user.

The POSIX library has pthread_join() to do this job.  It was always
expected that if a user needed these sorts facilities then they would
use POSIX rather than the bare kernel.


However, combining the exit flag and cyg_thread_delete() seems to be a
reasonable user-level solution:

volatile bool my_thread_exited = false;

void my_thread( CYG_ADDRWORD arg )
{
    my_thread_exited = true;
    return;
}

void wait_for_my_thread()
{
    for(;;)
    {
        if( my_thread_exited )
            if( cyg_thread_delete( my_thread )
                return;
        cyg_thread_delay(1);
    }
}

-- 
Nick Garnett                    eCos Kernel Architect
http://www.ecoscentric.com/     The eCos and RedBoot experts


--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]