Select in Nim
Nim’s select
statement allows you to wait on multiple channel operations. Combining threads and channels with select is a powerful feature of Nim.
We receive the values “one” and then “two” as expected.
Note that the total execution time is only ~2 seconds since both the 1 and 2 second sleep
operations execute concurrently.
In this Nim version, we use threads instead of goroutines, and we create channels using the Channel
type. The select
statement in Nim works similarly to Go’s, allowing us to wait on multiple channel operations.
We define two worker procedures that sleep for different durations before sending a message on their respective channels. These are executed in separate threads.
The main loop uses select
to wait for messages from either channel, printing them as they arrive. After the loop, we join the threads and close the channels to clean up resources.
This example demonstrates how Nim can handle concurrent operations and channel communication in a way similar to Go, albeit with some syntax and conceptual differences.