我怎么知道一个门厅是否完成,没有阻塞?

编程入门 行业动态 更新时间:2024-10-26 14:38:39
本文介绍了我怎么知道一个门厅是否完成,没有阻塞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 到目前为止,我所见过的所有示例都涉及阻塞以获取结果(通过< -chan 运算符)。

我现在的方法是将一个指针传递给一个struct:

type goresult struct { result与resultType; 完成了bool; }

在goroutine完成时写入。然后,只要方便,只需检查完成即可。你有更好的选择吗?

我真正想要的是一个Qt风格的信号插槽系统。我有一个预感,解决方案看起来几乎是微不足道的( chan s有很多潜在未知空间),但我还不太熟悉你可以使用逗号,确定模式(请参阅 effective go ):

foo:=< - ch; //这块。 foo,ok:=< - ch; //这将立即返回。

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.

解决方案

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-02 13:28:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:门厅   我怎么

发布评论

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

>www.elefans.com

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