Kiran
Kiran

Reputation: 75

How to add delay in Micro secs in tcl file?

I am using when command in tcl file, and after the condition is met I want to wait for some microseconds. I have found after, but the delay we specify for after is in milliseconds; it is not taking decimal values.

So is there any other way to add short delay in tcl file?

Upvotes: 1

Views: 1261

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137557

There's no native operation for that. If it is critical, you could busy-loop looking at clock microseconds

proc microsleep {micros} {
    set expiry [expr {$micros + [clock microseconds]}]
    while {[clock microseconds] < $expiry} {}
}

I don't really recommend doing this as it is not energy efficient; such high precision waiting is rarely required in my experience (unless you're working on an embedded system with realtime requirements, an area where Tcl isn't a perfect fit).

Of course, you can also make a C wrapper round a system call like nanosleep(), and that might or might not be a better choice (and might or might not be more efficient)…

Upvotes: 1

Related Questions