你会如何做/写这个家庭作业? (理论)

发布于 2024-07-16 07:13:01 字数 846 浏览 5 评论 0原文

我并不是要求任何人为我做这项作业,但我提出它是因为它是对 C# 和线程的非常好的实用介绍,但同时我觉得它可能有点太简单了。

这真的是教授线程的最佳方法吗? 在此练习中“丢失”了哪些关键的线程概念,第一次使用线程的新程序员可能无法观察到什么?

我有很多关于线程的理论知识,但自己不必做很多事情过去,有人在写的时候对我有什么注意事项吗?

这是原始作业的链接

,这是目标文本:

1) 创建线程安全泛型 循环队列类并创建 GUI 使用它(参见下一节)。 在这个 上下文,线程安全意味着每个 改变的操作(方法) 队列的内容应该是 一次仅由一个线程执行 以避免数据损坏。 A 循环队列被实现为 固定大小数组的开头位置 和队列末尾是索引 数组。 当队列填满时, 队列的开头和结尾将 随着元素的变化而转移到更高的值 添加并最终环绕到 数组中要重用的第一个索引 记忆。 这门课还应该 抛出异常(如下指定) 如果操作是调用者 无效的。

2) 创建GUI来控制 生产者-消费者中的两个线程 时尚。 GUI 将能够启动 并启动和停止生产者 和消费者线程并控制 他们修改的速度 通用循环队列。

I'm not asking for anyone to do this homework for me, but I bring it up because it's a very good practical introduction to C# and threading, but at the same time I feel it's perhaps a little too simple.

Is this really the best way to teach threading? what key threading concepts are "lost" in this exercize, what would new programmers using threads for the first time likely fail to observe?

I have alot of theoretical knowledge about threading but haven't had to do alot of it myself in the past, does anyone have any caveats for me when writing it?

Here's the link to the original assignment

and here is the goals text:

1) Create a thread-safe generic
circular queue class and create a GUI
to use it (See next section). In this
context, thread safe means that each
operation (method) that changes the
contents of the queue should be
executed by only one thread at a time
in order to avoid data corruption. A
circular queue is implemented as a
fixed size array where the beginning
and end of the queue are indices in
the array. As the queue fills up, the
beginning and end of the queue will
shift to higher values as elements are
added and eventually wrap around to
the first index in the array to reuse
the memory. This class should also
throw an exception (specified below)
to the caller if the operation is
invalid.

2) Create a GUI to control
two threads in a producer-consumer
fashion. The GUI will be able to begin
and start and stop both the producer
and consumer threads and control the
rate at which they modify the
GenericCircularQueue.

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

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

发布评论

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

评论(3

春夜浅 2024-07-23 07:13:01

阅读有关 SyncLock 或 Monitor 的信息(假定为 Windows C#)。

这是理解多线程的好方法,尤其是在多核时代。

Read about SyncLock or Monitor assuming Windows C#.

It as good a way as any to understand multi-threading, especially in the day of multi-cores.

我早已燃尽 2024-07-23 07:13:01

我想说这忽略了两件大事:

  • 大量写入,读取无法获得锁定和饥饿; 结果:像这样的有界队列已满。 您需要一种方法来为读者提供更高的优先级,以便他们可以排空队列。
  • 可扩展性——您只需需要一个读或写锁就可以很容易地使它成为线程安全的,这对于一个读者/一个写者来说非常有用。 但是,一旦拥有大量生产者/消费者,就会出现大量线程争用。

I'd say that this misses two big things:

  • Lots of writes, reads can't get lock and starve; result: a bounded queue like this fills up. You need a way to give readers a higher priority so that they can drain the queue.
  • Scalability---you can make it thread-safe pretty easily by just requiring a lock to read or write, and that will work great for one reader/one writer. However, once you have large numbers of producers/consumers, you'll have a lot of thread contention.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文