{"size": 1507, "ext": "scrbl", "max_stars_repo_path": "scribblings/thread-with-id.scrbl", "max_stars_repo_name": "dstorrs/thread-with-id", "max_stars_repo_stars_event_min_datetime": "2020-06-19T17:40:31.000Z", "max_stars_repo_stars_event_max_datetime": "2020-06-26T01:56:29.000Z", "max_issues_repo_path": "scribblings/thread-with-id.scrbl", "max_issues_repo_name": "dstorrs/thread-with-id", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "scribblings/thread-with-id.scrbl", "max_forks_repo_name": "dstorrs/thread-with-id", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 35.0465116279, "max_line_length": 256, "alphanum_fraction": 0.6284007963}
#lang scribble/manual


@title{thread-with-id}

@author{David K. Storrs}

@(require (for-label racket "../main.rkt")
	  racket/sandbox
          scribble/example)

@defmodule[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.


@defparam[thread-id thread-id non-empty-string? #:value "thread-<number>"]{Contains a string of the form @racket["thread-<number>"] where @racket["<number>"] is a pseudo-randomly generated number.}

@defproc[(thread-with-id [thnk (-> any)]) non-empty-string?]{Generates a new thread ID and runs the provided thunk in a new thread with that thread ID parameterized in.}

@(define eval
   (call-with-trusted-sandbox-configuration
    (lambda ()
      (parameterize ([sandbox-output 'string]
                     [sandbox-error-output 'string]
                     [sandbox-memory-limit 50])
        (make-evaluator 'racket)))))

@examples[
          #:eval eval
          #:label #f
          (require thread-with-id)

          (displayln (format "In outer scope, (thread-id) is: ~v" (thread-id)))

          (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))]
