thread-with-id
thread-id
thread-with-id
9.4.0.1

thread-with-id🔗ℹ

David K. Storrs

 (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) → non-empty-string?

(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-1271110765"

> (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-2207508593"

In thread #1, (thread-id) is: "thread-3835058580"

In thread #2, (thread-id) is: "thread-3499262328"

In thread #3, (thread-id) is: "thread-3649498072"

In thread #4, (thread-id) is: "thread-314047728"