This is the mail archive of the ecos-discuss@sourceware.cygnus.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]

Re: Creating workers threads dynamically


Rosimildo daSilva wrote:
> 
> I have a question regarding a thread management, specifically killing itself
> or exiting. The documentation states that exiting a thread
> just suspend it. Looking at the code, I saw that it suspend the
> thread and goes to an infinite loop.

The loop isn't a busy loop of course - in practice the thread has
completely suspended.
 
> I'd like to create workers threads dynamic as we need them, and
> when they have completed their tasks, they should kill themselves.

They can exit, but the threads will still be registered with the scheduler,
with state EXITED. To completely get rid of the thread (so that the
thread's memory can be reused for example), you need to call the thread's
destructor. This is what is done in cyg_thread_delete().

But you can't call this from within the thread itself - if you did, the
thread would temporarily be in an odd state where it doesn't "exist" in the
scheduler. If the thread got timesliced, it would all go horribly wrong.
[1] 

So it has to be done by another thread. Perhaps just use 1 simple thread
that only has a single job - killing threads. A "reaper" thread like this
is a common idiom.

Or use static threads :-).

Jifl


[1] Actually there _is_ a way round this in the current implementation
which is just to lock the scheduler while deregistering the thread.
However, in future versions of the kernel, we are going to support the
kernel managing the stacks, so the stack would be freed then too, and
without a stack there's no hope :-).

-- 
Red Hat, 35 Cambridge Place, Cambridge, UK. CB2 1NS  Tel: +44 (1223) 728762
"Plan to be spontaneous tomorrow."  ||  These opinions are all my own fault

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