thread-with-id
thread-id
thread-with-id
8.12

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-2705091423"

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

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

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

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

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