我怎么知道一个门厅是否完成,没有阻塞?(How do I find out if a goroutine is done, without blocking?)

编程入门 行业动态 更新时间:2024-10-26 12:24:52
我怎么知道一个门厅是否完成,没有阻塞?(How do I find out if a goroutine is done, without blocking?)

到目前为止,我见过的所有例子都涉及阻塞来获取结果(通过<-chan运算符)。

我目前的做法涉及将指针传递给结构:

type goresult struct { result resultType; finished bool; }

该门厅在完成时写入。 然后,只要方便,就可以finished检查。 你有更好的选择吗?

我真正想要的是一个Qt风格的信号插槽系统。 我有预感,解决方案看起来几乎微不足道(有很多未知的潜力),但我还不熟悉这门语言。

All the examples I've seen so far involve blocking to get the result (via the <-chan operator).

My current approach involves passing a pointer to a struct:

type goresult struct { result resultType; finished bool; }

which the goroutine writes upon completion. Then it's a simple matter of checking finished whenever convenient. Do you have better alternatives?

What I'm really aiming for is a Qt-style signal-slot system. I have a hunch the solution will look almost trivial (chans have lots of unexplored potential), but I'm not yet familiar enough with the language to figure it out.

最满意答案

你可以使用“逗号,好的”模式(参见他们的“ 有效的去 ”页面):

foo := <- ch; // This blocks. foo, ok := <- ch; // This returns immediately.

You can use the "comma, ok" pattern (see their page on "effective go"):

foo := <- ch; // This blocks. foo, ok := <- ch; // This returns immediately.

更多推荐

本文发布于:2023-08-06 17:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454939.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:门厅   我怎么   find   goroutine   blocking

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!