8.16.0.1
thread-with-id
(require thread-with-id) | package: thread-with-id |
When working in a multi-threaded environment it can be useful to be able to distinguish which actions are coming from which thread, especially when ‘action’ means ‘log message being printed’. This module provides a parameter and a function to manage this.
parameter
(thread-id thread-id) → void? thread-id : non-empty-string?
= "thread-<number>"
Contains a string of the form "thread-<number>" where "<number>" is a pseudo-randomly generated number.
procedure
(thread-with-id thnk) → non-empty-string?
thnk : (-> any)
Generates a new thread ID and runs the provided thunk in a new thread with that thread ID parameterized in.
> (require thread-with-id) > (displayln (format "In outer scope, (thread-id) is: ~v" (thread-id))) In outer scope, (thread-id) is: "thread-1623469718"
> (define (show-example n) (void (sync (thread-with-id (lambda () (displayln (format "In thread #~a, (thread-id) is: ~v" n (thread-id)))))))) > (for ([i 5]) (show-example i))
In thread #0, (thread-id) is: "thread-3991447421"
In thread #1, (thread-id) is: "thread-4202934239"
In thread #2, (thread-id) is: "thread-2727949896"
In thread #3, (thread-id) is: "thread-1386934366"
In thread #4, (thread-id) is: "thread-949984628"