Using TChan with Timeout

前端 未结 3 648
谎友^
谎友^ 2021-02-14 19:49

I have a TChan as input for a thread which should behave like this:

If sombody writes to the TChan within a specific time, the content should be retrieved. If there is n

3条回答
  •  有刺的猬
    2021-02-14 20:08

    Instead of TChan a, use TChan (Maybe a) . Your normal producer (of x) now writes Just x. Fork an extra "ticking" process that writes Nothing to the channel (every x seconds). Then have a reader for the channel, and abort if you get two successive Nothing. This way, you avoid exceptions, which may cause data to get lost in your case (but I am not sure).

提交回复
热议问题