从不同线程读取、删除和添加元素到链表

发布于 2025-01-03 23:14:43 字数 670 浏览 0 评论 0原文

对于这种情况,最佳编程实践是什么:

我有一个包含文件对象的链接列表,偶尔会将传入的文件对象添加到此列表中(AddThread)。

这些文件对象需要“处理”,据说是由另一个线程(ProcessThread)进行的。因此,每当将文件对象添加到列表中时,都需要一次处理一个文件对象,而其他文件对象仍然可以添加到列表中。

在处理过程中,会读取列表中的项目,然后将其从该列表中删除。只要列表为空,处理就会停止,如果添加新项目,处理就会重新开始。

我的看法:

每当对列表应用更改(添加项目)时,就会触发更改事件。此事件唤醒ProcessThread。 ProcessThreads 将列表锁定一小段时间以读取它,然后再次释放锁定。当处理文件对象时,它会再次锁定它以快速从列表中删除该项目并解锁它。 当列表中不再有文件对象时,ProcessThread 将设置为睡眠状态。

这是正确的方法吗,还是还有其他更好的选择..?

Greets Daan

编辑:我使用链接列表,因为这样我可以在列表的开头或结尾添加一个文件对象,以便在其他列表之前/之后对其进行处理。处理最多可能需要一分钟,具体取决于网络负载。有时会失败,需要重新处理。

What would be the best programming practice for this case:

I have a linkedlist with file-objects, once in a while incoming file objects are added to this list (AddThread).

These file-objects need to be "processed", supposedly by another thread (ProcessThread). So whenever file-objects are added to the list, they need to be processed one at a time, while other file-objects still can be added to the list.

During processing, an item from the list is read and afterwards removed from that list. Processing stops whenever the list is empty, and starts again if new items are added.

How I see it:

Whenever a change is applied to the list (item added), a change event is fired. This event wakes up the ProcessThread. The ProcessThreads locks the list for a small amount of time to read it and releases the lock again. When the file-object is processed it locks it again to quickly remove the item from the list and unlocks it.
When there are no file-objects anymore in the list, the ProcessThread is set to sleep.

Is this the right way to do it, or are there other better options..?

Greets Daan

Edit: I use a linkedlist, because then I can add a file-object on the beginning or the end of the list, so that it is processed before/after others. Processing can take up to a minute, depending on the networkload. Sometimes it will fail, and it needs to be processed again.

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

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

发布评论

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

评论(3

↘人皮目录ツ 2025-01-10 23:14:43

好的,在阅读其他帖子和更新后,两个“普通”队列(低优先级和高优先级)、一个互斥锁/CS 锁和一个信号量组成一个优先级阻塞队列怎么样?要添加文件对象,请获取锁,将文件对象推送到队列之一,释放锁,向信号量发出信号。在进程线程中,等待信号量,获取锁,尝试从高优先级队列中获取,如果失败,则从低优先级队列中获取,然后释放锁并处理文件对象。

OK, after reading the other posts and updates, how about two 'plain' queues, (low and high priority), a mutex/CS lock and a semaphore, making up a priority blocking queue? To add a file-object, acquire the lock, push the file-object onto one of the queues, release the lock, signal the semaphore. In the process-thread/s, wait on the semaphore, acquire the lock, TryTake from the high-priority queue and, if this fails, Take from the low-priority queue, then release the lock and process the file-object.

墨小沫ゞ 2025-01-10 23:14:43

也许您想使用 BlockingCollection。
http://msdn.microsoft.com/en-us/library/dd267312.aspx

*如果您从队列中取出一个元素并且队列为空,则线程将阻塞,直到队列中有元素为止。

*如果向队列添加一个元素并且队列已满,则线程将阻塞,直到有空间为止。

*它是线程安全的。

maybe you want to use the BlockingCollection.
http://msdn.microsoft.com/en-us/library/dd267312.aspx

*If you take an element from the queue and the queue is empty the thread will block until there is something in the queue.

*If you add an element to the queue and the queue is full the thread will block until there is space.

*It is thread safe.

蓝梦月影 2025-01-10 23:14:43

如果您使用 .net 4.0,我会考虑 ConcurrentQueue 容器。

这使您可以线程安全地访问对象队列的任一端,您无需担心锁定机制的实现。

至于启动进程线程,我会将其视为一个单独的问题。根据您的描述,事件听起来是正确的方法,但轮询或事件驱动的解决方案是否最合适取决于队列可能被激活的频率、过程的速度、您需要系统的响应速度。

另一件事。为什么不在开始处理时从列表中删除文件对象呢?处理是否有可能失败并且您需要重试?如果您将该项目保留在列表中,您是否有可能运行 2 个处理线程并处理它两次?根据描述不可能说,但值得考虑。

If you're using .net 4.0 I would consider the ConcurrentQueue container.

This gives you thread safe access to either end of a queue of objects, you wouldn't need to worry yourself about the implementation of the locking mechanism.

As far as firing up the process thread, I would look on this as a separate issue. An event sounds like the right way to go based on your description but whether a polling or event driven solution is most appropriate depends on how often the queue is likely to be activated, how fast the process is, how responsive you need the system to be.

One other thing. Why don't you just remove the file-object from the list when you begin processing? Is there a chance the processing may fail and you'll need to try again? If you leave the item in the list is there a chance you might run 2 processing threads and process it twice? It's impossible to say based on the description but worth considering.

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