Thread ctors/dtors

dsimcha dsimcha at yahoo.com
Wed Apr 29 17:18:23 PDT 2009


== Quote from Sean Kelly (sean at invisibleduck.org)'s article
> == Quote from dsimcha (dsimcha at yahoo.com)'s article
> > Would it be easy to put into core.thread a mechanism that would guarantee that
> > a set of callback functions were called from any newly created thread upon
> > creation, or by any thread just before termination?  Of course, by thread
> > termination, I mean if the thread terminates normally because it finishes its
> > job, not if it gets killed externally.
> It seems like this would be easy to do in an app without modifying the runtime.
> Or is there some need to hook the initialization and termination of threads you
> don't own?

I'm talking about if you write some generic lib that needs something like this,
and you don't want the users of the lib to have to do it manually every time they
create a new thread.  For example, let's say you write a random number generation
module, which has a thread-local global random number generator.  This generator
is supposed to "just work" without the user having to do anything.  The way the
new Phobos handles this is to check an internal flag.  A better way would be to
add a callback that seeds the new random number generator every time a new thread
is created.

Of course there are probably plenty of similar situations with less trivial
workarounds.  This is just supposed to be an illustrative example.



More information about the Digitalmars-d mailing list