[plt-dev] thread-specifics [PATCH]
Is there a reason this needs to be built in, instead of implemented as
a weak hash table?
At Tue, 3 Mar 2009 14:31:33 -0500 (EST), Dimitris Vyzovitis wrote:
> The attached patch adds two new atomic primitives implementing
> thread-specifics:
> (thread-get-specific thread? (undefined (lambda () #f)) -> any?
> Retrieve the specific value from a thread.
> The optional undefined thunk is used to provide a value when the
> specific has not been set by the target thread.
> (thread-set-specific! any?) -> void
> Set the specific value for the current thread.
>
> These primitives are useful for communicating publicly visible information
> and simplify common synchronization patterns.
>
> Some simple examples in the test module [thread-specific.ss]:
> async: an asynchronous computation wrapper around threads.
> tagged: a state machine that tags its current state.
> my-thread: a synchronous thread spawner that places an ownership mark.
>
> test run:
> > (require "test-threadsp.ss")
> > (run-tests)
>
> (sync (async a1))
> => 1
> ; correct
>
> (exn? v2)
> => #t
> ; correct
>
> (thread-get-specific tag)
> => b1
> ; correct
>
> (thread-get-specific tag)
> => b2
> ; correct
>
> (thread-get-specific tag)
> => b3
> ; correct
>
> (my-thread? (thread void) mark)
> => #f
> ; correct
>
> (my-thread? (my-thread void mark) mark)
> => #t
> ; correct
>
> -- vyzo