Hi, I'd like to have two threads in my application, one GUI thread and one network thread. I was thinking of running a asyncdispatch Dispatcher on the network thread, and the GTK main loop on the main one.
Now, I'd like to signal between both threads. How can I post a task to run on the Dispatcher, from the GUI thread? I can post a task onto the GUI thread by calling g_idle_add. In twisted, I could use callFromThread from another thread to schedule something on it's loop. What is the equivalent in Nim?
I looked at channels, but they seem to be impossible to use since I already have an event loop running in each thread.
Looking at the implementation of sleepAsync, I thought about adding a timer with 0ms, but I can't access the thread's dispatcher from outside the thread, nor the timers field of the dispatcher. Even if I could, there are no locks, so there is no way to safely add a timer from outside.
I could set up a socket to communicate between threads, but that seems ridiculous, and it also counters my goal of keeping networking off the GUI thread. (Even async-style networking. I've found, at least with twisted, that there still is enough blocking going on to make the GUI noticeably less responsive.)
The last thing I can think of is to set up some kind of manual polling on the network thread, but that also seems wrong since there's already a kind of select call going on there.
Hey,
I think you will need to use channels to communicate between the threads. You can use sleepAsync to check whether there is something on the channel periodically. Channels do not support async await unfortunately, but it would be nice if they did.