Continuo lo studio del favoloso Racket, oggi capitolo nuovo, copio da qui:
Racket provides concurrency in the form of threads, and it provides a general
sync function that can be used to synchronize both threads and other implicit forms of concurrency, such as ports.
Threads run concurrently in the sense that one thread can preempt another without its cooperation, but threads do not run in parallel in the sense of using multiple hardware processors. See Parallelism [prossimamente] for information on parallelism in Racket.
To execute a procedure concurrently, use
thread. The following example creates two new threads from the main thread:
mi sa che conviene usare DrRacket.
The next example creates a new thread that would otherwise loop forever, but the main thread uses
sleep to pause itself for
2.5 seconds, then uses
kill-thread to terminate the worker thread:
Note: In DrRacket, the main thread keeps going until the Stop button is clicked, so in DrRacket the
thread-wait is not necessary.
If the main thread finishes or is killed, the application exits, even if other threads are still running. A thread can use
thread-wait to wait for another thread to finish. Here, the main thread uses
thread-wait to make sure the worker thread finishes before the main thread exits:
Each thread has a mailbox for receiving messages. The
thread-send function asynchronously sends a message to another thread’s mailbox, while
thread-receive returns the oldest message from the current thread’s mailbox, blocking to wait for a message if necessary. In the following example, the main thread sends data to the worker thread to be processed, then sends a
'done message when there is no more data and waits for the worker thread to finish.
lo stesso file eseguito nel terminale:
In the next example, the main thread delegates work to multiple arithmetic threads, then waits to receive the results. The arithmetic threads process work items then send the results to the main thread.
naturalmente meglio nel terminale