Re: [AD] public thread API

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On 2008-08-07, Elias Pschernig <elias@xxxxxxxxxx> wrote:
> On Thu, 2008-08-07 at 22:37 +1000, Peter Wang wrote:
> > On 2008-08-06, Elias Pschernig <elias@xxxxxxxxxx> wrote:
> > > On Wed, 2008-08-06 at 11:04 +1000, Peter Wang wrote:
> > > > 
> > > > Taking it a bit further, al_create_thread could not just allocate the
> > > > space for the thread handle, but call pthread_create.  But we would not
> > > > actually execute the user's thread function until al_start_thread.
> > > > I believe this is useful for cases like:
> > > > 
> > > >     T = al_create_thread(thread_func);
> > > >     do further initialisation, using T
> > > >     al_start_thread(T);
> > > > 
> > > > Not sure how common it is.
> > > > 
> > > 
> > > I was thinking about creating the threads first, then you could run them
> > > multiple times, then destroy them. (So would not call pthread_create I
> > > think.)
> > 
> > Any particular use for that?
> > 
> 
> Well, nothing which couldn't be done in another way as well. But it
> would be straightforward to implement using the current internal API,
> while I have no idea how calling pthread_create without running the
> function would be done with it.

Instead of starting the user's thread function in the new thread you
call a wrapper function.  The wrapper waits on a condition which is
signalled when al_start_thread() is called.  Then it proceeds with the
user's function.  Of course the user could do the same thing himself.

Another good thing (similar to what Peter Hull said) is that you
only need to worry about threads failing to spawn at al_create_thread()
calls.  If you need multiple threads, you can al_create_thread them all,
check they all spawned correctly, then start running them together.
If some threads spawned and some didn't, it's more difficult to clean up
the ones that started if they're off and running already.

Peter





Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/