Forum OpenACS Q&A: Halting a thread for a specified time

Hi... I have a .tcl file which is going to be a detached thread
(using 'ns_thread begindetached'). In the file I have a procedure
which has an infinite loop ('while{ 1 }') but I want the loop to
only loop around every 1 minute. How could I do this?
Collapse
Posted by carl garland on
Just put a ns_sleep command for 60 sec or however long you want at end of loop. Of course I would maybe add a global nsv that also checks to make sure you want to loop forever so you will be able to more cleanly shutdown ...
Collapse
Posted by Andrew Piskorski on
E.g., put something along these lines in mypackage-init.tcl:

if { ![nsv_exists . shutdown_pending] } { 
   nsv_set . shutdown_pending 0 
   ns_atshutdown [list nsv_set . shutdown_pending 1] 
}

Then in your detached thread do something like:

while { ![nsv_get . shutdown_pending] } { 
   ns_sleep 60 
   # Etc. 
}