adamdruppe / arsd

This is a collection of modules that I've released over the years. Most of them stand alone, or have just one or two dependencies in here, so you don't have to download this whole repo.
http://arsd-official.dpldocs.info/arsd.html
531 stars 125 forks source link

Q: does the Timer class have to be used with SimpleWindow & window.eventLoop() ? #392

Closed mw66 closed 1 year ago

mw66 commented 1 year ago

Hi,

I'm reading the doc:

http://arsd-official.dpldocs.info/arsd.simpledisplay.html#topic-timers http://arsd-official.dpldocs.info/arsd.simpledisplay.Timer.html

I'm wondering if the Timer class have to be used with SimpleWindow & window.eventLoop() ?

If my app is not a GUI app, how can I use this timer?

Or do you know another library that provide a non-GUI Timer class?

Thanks.

adamdruppe commented 1 year ago

It doesn't need a window but it does need an event loop - timers usually do. You don't have to create a window though, you can also use EventLoop.get.run() ... or whatever I caleld it.

mw66 commented 1 year ago

Thanks, this is working:

#!/usr/bin/env dub

/+dub.sdl:
dependency "arsd-official:simpledisplay" version="~>10.9.10"
+/

import std;
import arsd.simpledisplay;

void main() {
    auto timer = new Timer(1000, delegate {
            writeln(1);
    });
    EventLoop.get.run();
}

My next question: is the Timer and EventLoop thread based or signal based? (I'm using 64-bit Linux).

I am asking because I need to worry about the thread-safety of what I call inside the delegate.

adamdruppe commented 1 year ago

Neither, it is either a file descriptor or a timeout argument to epoll on linux. The Timer class I believe is a timerfd, but I use both in different places.

So inside the event loop the timer callback is queued and it is always run in the one thread. (I am actually working on changing this code rn but this part won't change anyway, my default is always to run the callback in the same thread that is running the loop that created it)

mw66 commented 1 year ago

So inside the event loop the timer callback is queued and it is always run in the one thread. ... default is always to run the callback in the same thread that is running the loop that created it

Thanks, I think it worth adding this info into the doc, for people who worried about thread-safety of the delegate.