How implement go style channels (CSP) with objective-c?

huangapple go评论80阅读模式
英文:

How implement go style channels (CSP) with objective-c?

问题

我想知道如何为Objective-C创建一个类似于Go语言中的通道/协程的CSP库,但要使用Objective-C的惯用方式(比实际方法少一些样板代码)。

在其他具有本地协程和/或生成器的语言中,可以很容易地建模,但我不知道如何在Objective-C中使用几种并发编程方法来实现相同的效果(此外,想要实现“廉价”线程的想法)。

有关我需要做什么的任何提示吗?

英文:

I wonder how create a CSP library for obj-c, that work like Go's channels/goroutines but with idiomatic obj-c (and less boilerplate than actual ways).

In other languages with native courutines and/or generators is possible to model it easily, but I don't grasp how do the same with the several ways of do concurrent programing in obj-c (plus, the idea is have "cheap" threads).

Any hint about what I need to do?

答案1

得分: 1

我会查看State Threads库,因为它实现了与Go的goroutine切换算法基本相同的思想:当goroutine在系统调用中即将休眠时,它将控制权交给调度器,因此ST库封装了操作系统级的文件描述符,提供了自己的类似FD的对象,可以从中读取(和/或写入),但是这些操作不会阻塞整个进程,而是将控制权转移到由库管理的其他轻量级线程。

然后,您可能需要一个比ST库更先进的调度器来保持操作系统线程忙于运行您的SP。关于Go 1.2调度器的简单介绍可以在这里找到,并且其中包含了一个更深入的设计文档的链接。其余的内容可以在Go的源代码中找到。

还可以参考Stack Overflow上的这个答案

英文:

I would look at the State Threads library as it implements roughly the same idea which underlies the goroutine switching algorythm of Go: a goroutine surrenders control to the scheduler when it's about to sleep in a syscall, and so the ST library wraps OS-level file descriptors to provide their own FD-like objects which can be read from (and/or written to) but instead of blocking the whole process these operation transfer control to other light-weight threads managed by the library.

Then you might need a scheduler more advanced than that of the ST library to keep OS threads busy running your SPs. A no-brainer introduction to the Go 1.2 scheduler is here, and it contains a link to a more hard-core design document. The rest is in the Go's source code.

See also this answer on SO.

答案2

得分: 0

创建操作,例如考虑以下过程:

  • 过程 xeast 获取数字,将其转换为字符串,并将其传递给 west

我可以使用一个对象来模拟它,该对象保持 x 的内部状态(包括数字和字符串),并具有以下操作:

  • east-output,由 east 过程逻辑在其他地方定义的操作
  • x-input,依赖于 east-output。它将数字从 east-output 的数据结构复制到 x 的数据结构中
  • x-output,依赖于 x-input。它的内容被定义为纯粹的内部转换 - 在我们的示例中,是 stringWithFormat...
  • west-input,依赖于 x-output,等等。

然后,将这些操作转储到 NSOperationQueue 中,看看会发生什么(是否工作,或是否存在相互矛盾的依赖关系...)

英文:

Create operations, e.g. for an example consider this process:

  • process x takes number from east, transforms it to a string, and gives it to west.

That I could model it with an object that keeps an internal state of x (consisting of number and string) and the following operations:

  • east-output, operation defined somewhere else by east process logic
  • x-input, operation that depends on east-output. It copies number from east-output's data structure into x's data structure
  • x-output, operation that depends on x-input. Its content is defined as purely internal transformation - in our example, stringWithFormat...
  • west-input, operation that depends on x-output, etc.

Then you dump the operations into NSOperationQueue and see what happens (does it work, or are there contradicting dependencies...)

huangapple
  • 本文由 发表于 2013年9月9日 00:06:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/18685776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定