Thread Sleep Duration

The sleep function blocks the currently running thread, causing it to suspend for an at least specified duration before resuming execution. The parameter type is Duration. Its prototype is as follows:

func sleep(dur: Duration): Unit // Sleep for at least `dur`.

Note:

If the value of dur is less than or equal to that of Duration.Zero, the current thread yields execution resources but does not enter the sleep state.

The following is an example of using sleep:

import std.sync.*
import std.time.*

main(): Int64 {
    println("Hello")
    sleep(Duration.second)  // sleep for 1s.
    println("World")
    return 0
}

The output is as follows.

Hello
World