Correct use of Timer

I’m making use of Timer with a callback function to create a simple (?) purpose built cache-mechanism where large objects are moved to disk a short while after they have been used and I’m starting to feel that famous sense of precariousness related to asynchronity.

At first I thought that not using multithreading would make things easier to reason about. However, the “main task” code is pretty generic and sometimes user specified so I suppose one must assume that the progam might yield at any time (e.g. through printing/logging, other IO, CUDA?).

Does this mean that one pretty much must treat the callback function as running on a separate thread w.r.t locking etc.?

Conversely, I assume that one must make sure that the main code yields every now and then or else the callback function might never be called.

Given these two, would it just be better to put the Timer in a separate thread to begin with so I don’t need to worry about putting yield in the right places, or will that expose new problems not present when using a coroutine?

Is there a correct way to start a timer on a thread? Threads.@spawn Timer(delay) do t ... ?

1 Like