多线程谜题
我正在尝试提出一些专注于多线程的编程难题。到目前为止,我能够提出的大多数问题都是特定领域的。对于尝试学习多线程应用程序核心概念的开发人员来说,是否有人有任何像样的编程难题?
I'm trying to come up with some programming puzzles focused on multi-threading. Most of the problems I've been able to come up with, so far, have been pretty domain specific. Does anybody have any decent programming puzzles for developers attempting to learn the core concepts of multi-threading applications?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
此链接涵盖了许多主题。
使用 ThreadMentor 进行多线程编程:教程
<强>编辑:
以下是该链接中列出的问题的一些直接链接及其初始描述。
ThreadMentor:餐饮哲学家的问题
ThreadMentor:餐饮哲学家的问题:左-右版本
ThreadMentor:吸烟者的问题
ThreadMentor:生产者/消费者(或有界缓冲区)问题
ThreadMentor:过山车问题
这辆车还有额外的限制:
ThreadMentor:桥梁问题
这个问题的描述依赖于图像。这是删除了图像引用的修改后的报价。
There are a number of topics covered at this link.
Multithreaded Programming with ThreadMentor : A Tutorial
Edit:
Here are some direct links to the problems listed at that link, along with their initial descriptions.
ThreadMentor : The Dining Philosopher's Problem
ThreadMentor : The Dining Philosopher's Problem: The Lefty-Righty Version
ThreadMentor : The Cigarette Smoker's Problem
ThreadMentor : The Producer/Consumer (or Bounded-Buffer) Problem
ThreadMentor : The Roller Coaster Problem
This one has additional constraints:
ThreadMentor : The Bridge Problem
The description for this one relies on images. Here is a modified quote with image references removed.
用餐哲学家问题,是我第一个想到的问题。
The Dining Philosophers Problem, is the first one I think of.
你的内存中有一个很大的树结构。许多线程需要搜索该结构。有时,线程需要在结构中插入或删除某些内容。如何控制对结构的访问,以便程序能够正确运行(在更改结构时没有两个线程会相互干扰)并且高效(线程在不必要时不会被阻塞)?
You have a large tree structure in memory. Many threads need to search the structure. Occasionally, a thread will need to insert or remove something from the structure. How do you control access to the structure so that the program will run correctly (no two threads will stomp on each other while changing the structure) and efficiently (no threads are blocked when they don't have to be)?
睡觉理发师问题浮现在脑海中,吸烟者问题。
The Sleeping Barber Problem springs to mind, as does the Cigarette Smokers Problem.
哲学家就餐就是其中之一...
男女通用浴室是另一间
Dining philosophers is one...
unisex bathroom is another one
也许您可以使用测试和设置共享标志或以某种顺序一致的方式访问某种列表资源的简单问题?
Perhaps you can use the simple problem of testing and setting a shared flag or accessing some kind of list resource in some kind of sequentially consistent manner?
生产者-消费者问题。
The Producer-Consumer problem.
这是我用多线程完成的第一个问题,回来在我本科学习期间。
Here's the first problem I ever completed with multi-threading, back during my undergraduate studies.
电梯模拟器非常常见。
An Elevator Simulator is pretty common.
根据您使用多线程所做的事情,这会有所不同。
你在银行。平均每 2 分钟就有 1 名顾客到达。平均每名顾客在 2 分钟内得到服务。
哪种解决方案可以更好地服务客户?一根公用线,还是每个柜员一根线?
您的选择是否足以保证线路长度的一定限制?
答案:由于顾客到达的马尔可夫特性和每个人的实际服务时间,线路永远不会有界限。此外,让他们排成一队等待也是一个好主意,尽管这不足以克服无边无际的排队。
Depending upon what you are doing with your multi-threading, this makes a difference.
You are in a bank. Customers arrive at an average rate of 1 every 2 minutes. Each customer is served, on average, in 2 minutes.
Which is the better solution to serving the customers? One common line, or one line for each teller?
Is your choice enough to guarantee some bound on the length of the line?
Answers: because of the markov property of customer arrival and actual service time per individual, the line will never know a bound. additionally, it's a good idea to make them wait in one common line, although this is not enough to overcome the boundless line.
这是在 PARLANSE 中实现的并行 N 谜题求解器。该语言具有类似 LISP 的语法,但实际上更接近 C(标量、结构、指针、函数调用),但与 C 不同的是,它具有本地作用域。秘密在于并行 fork-grain 运算符 (|| ... ),它并行执行其所有操作数,以及 PARLANSE 使用异常来停止父 Grain 的能力。
该解算器在我尝试过的所有 4 路和 8 路机器上都能提供线性加速。
Here's a parallel N-puzzle solver implemented in PARLANSE. The language has a LISP-like syntax but is really closer to C (scalars, structs, pointers, function calls), but unlike C has local scopes. The secret is in the parallel fork-grain operator (|| ... ) which executes all of its operands in parallel, as well as PARLANSE's ability to use exceptions to stop parent grains.
This solver delivers linear speedups on all the 4 and 8 way machines on which I have tried it.
信号量小书是免费提供的书,其中有很多同步难题。它几乎包含了其他答案中引用的所有谜题。为所有谜题提供了解决方案。
The little book of semaphores which is freely available book has lots of synchronization puzzles. It includes almost all the puzzles cited in other answers. Solutions are provided for all the puzzles.