thread-with-id
thread-id
thread-with-id
7.8

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

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

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

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

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

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