ThreadId

Simon Marlow simonmar@microsoft.com
Wed, 27 Jun 2001 11:25:58 +0100


> How much of a thread's memory is kept alive - after the
> thread has terminated - as long as a program holds on to the
> thread's `ThreadId'?  The following ToDo comment in
> `PrelConc' seems to indicate that it is currently more than
> we really want:
>=20
>   data ThreadId =3D ThreadId ThreadId#
>   -- ToDo: data ThreadId =3D ThreadId (Weak ThreadId#)
>   -- But since ThreadId# is unlifted, the Weak type must use open
>   -- type variables.
>=20
> In combination with finalisers, this can be troublesome.

At the moment, the thread is kept completely alive by holding the
ThreadId.  Doing the weak pointer thing doesn't seem too hard.
Something like this should work:

  data ThreadId =3D ThreadId (Weak# ThreadId#)

  mkThreadId :: ThreadId# -> IO ThreadId
  mkThreadId t =3D IO $ \s ->=20
    case mkWeak# t t (unsafeCoerce# 0#) s of (# s, w #) ->
    (# s, ThreadId w #)

but you'll also need to update the other operations on ThreadIds.

Cheers,
	Simon