lundi 24 août 2020

Simplest way to switch context back and forth (dispatching)

I have two classes that perform independent computation. For the sake of simplicity here, I will represent them with functionscalc1 and calc2. At some random places in each function, I need to sleep for a while. Rather than doing calc1 and then calc2, I'd like to switch back and forth between them.

I could use threads, but it seems to me at first sight overly engineered to do so. I do not need different threads. I just need to switch from one context to another, and back where we were.

func calc1() {
...
sleep(300) // go to calc2
...
}

func calc2() {
...
sleep(200) // resume calc1
...
}

Is there a simple way to do this?

Aucun commentaire:

Enregistrer un commentaire