什么是“Hello World”?并发程序?

发布于 2024-12-08 17:52:41 字数 368 浏览 3 评论 0原文

我正在寻找一些规范的、简单的并发问题,适合演示我正在处理的并发计算库的使用情况。

为了澄清我所说的“并发”的含义:我对利用非确定性通信过程的算法感兴趣,而不是通过将工作分散到多个处理器来使快速排序等算法运行得更快。 就是我使用该术语的方式。

我知道哲学家就餐问题,这是可以接受的,但我想知道是否有任何问题更有说服力但同样简单的问题。

I'm looking for some canonical, simple concurrency problems, suitable for demonstrating usage of a library for concurrent computations I'm working on.

To clarify what I mean by "concurrency": I'm interested in algorithms that utilize non-deterministic communicating processes, not in e.g. making algorithms like quicksort run faster by spreading the work over multiple processors. This is how I'm using the term.

I know about the Dining Philosophers Problem, and that would be acceptable, but I wonder whether there are any more convincing but equally simple problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

年少掌心 2024-12-15 17:52:41

我一般使用简单的“银行账户转账”场景。例如,我在这个关于事务的问题中发布了一个这样的小案例。

这是一个很好的阐述案例,因为:

  • 每个人都了解业务问题。
  • 它强调并发环境中事务的重要性。
  • 您可以轻松扩展该场景(例如,如果您想在交易发生时计算所有当前帐户余额的总和,该怎么办?)

为了演示您的并发库,您可以在这种场景中启动一个运行数百万个交易的线程,并演示其他线程如何仍然可以看到一致的世界观等。

I generally use a simple "bank account transfer" scenario. For example I posted one such trivial case in this question on transactions.

It's a good case for exposition because:

  • Everyone understands the business problem.
  • It emphasises the importances of transactions in a concurrent environment.
  • You can easily extend the scenario (e.g. what if you want to calculate the sum of all current account balances while transactions are taking place?)

To demonstrate your concurrency library, you could probably start a thread running millions of transactions in this kind of scenario, and demonstrate how other threads can still see a consistent view of the world etc.

铜锣湾横着走 2024-12-15 17:52:41

我认为没有一个标准的第一个程序可以证明并发性正在工作,就像顺序程序的“Hello world”一样。

更典型的并发性是表现出问题的程序,例如并发计数器在没有正确同步的情况下丢失一些计数。或者银行账户之间的随机转账,如果简单地进行锁定,就会导致死锁。 (我在玩 Java 并发性时做了这些。)

演示并发性且相对简单的一件事是协作计数:并发线程(或其他线程)有一个内部计数器,它们相互发送该计数器,并将其设置为它们想要的值。收到加一。 (几年前,我用三个 LEGO Mindstorms RCX 通过红外线做到了这一点,效果很好。)

顺便说一句:嵌入式编程器的“Hello world”是闪烁的 LED。

I don't think there is a standard first program for demonstrating that concurrency is working, like "Hello world" for sequential programs.

More typical for concurrency are programs that demonstrate problems, for example concurrent counters that lose some counts without proper synchronization. Or random transfers between bank accounts that cause a deadlock if locking is done naively. (I did these when playing with Java concurrency.)

One thing that demonstrates concurrency and is relatively simple is to count cooperatively: The concurrent threads (or whatever) have an internal counter, which they send out to each other, and set to what they receive plus one. (I did that with three LEGO Mindstorms RCX over infrared some years ago, worked nicely.)

BTW: The "Hello world" of the embedded programmer is the blinking LED.

↘紸啶 2024-12-15 17:52:41

曾经有一个 Java 小程序示例(很可能仍然如此),您可以用它来测试 JVM 和底层操作系统使用的调度算法。它对两个(或可选更多?不记得)条形逐渐填充进行动画处理,每个条形由具有相同优先级的不同线程进行动画处理。

在我看来,在控制台上打印:等的等效项

red 1
red 2
green 1
red 3
green 2

在精神上最接近“hello,world”的本质。即“我能让计算机做一些无用但可见的事情吗?”

因此,在每个线程中,您需要一系列暂停(忙循环或睡眠,由您决定,您选择的暂停可能会影响输出,具体取决于您的并发安排方式),每个暂停后面都有一些输出。您可能想要同步输出——这并不是真正必要的,但是如果调度程序将一行打断,那么阅读起来就会很困难。

然后,如果您的并发模型是协作的(新石器时代的线程,或者可能是基于协同例程的模型),您还必须添加合适的产量,以防止在绿色条开始之前填充红色条。这告诉您已经成功地使并发代码交错。

There used to be a sample Java applet (quite possibly still is) that you used to test what scheduling algorithm your JVM and underlying OS use. It animated two (or optionally more? can't remember) bars gradually filling up, each animated by a different thread at the same priority.

An equivalent that prints:

red 1
red 2
green 1
red 3
green 2

etc to the console, seems to me to be the closest thing in spirit to the bare bones nature of "hello, world". That is, "can I make the computer do something useless but visible?"

So in each thread you'd want a series of pauses (either busy-loops or sleeps, up to you, and which you choose might affect the output depending how your concurrency is scheduled), each followed by some output. You might want to synchronize the output -- not really essential, but if a line were to be broken up by the scheduler it would be awkward to read.

Then if your concurrency model is co-operative (either neolithic threads, or perhaps something co-routine-based), you have to add suitable yields as well, to prevent the red bar filling before the green bar starts. That tells you that you've successfully made your concurrent code interleave.

前事休说 2024-12-15 17:52:41

您可以在单独的线程中对“Hello”和“World”进行光线追踪。
或者在“World”进行光线追踪时为“Hello”设置动画。

You can ray trace "Hello" and "World" in separate threads.
Or animate "Hello" while "World" is raytracing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文