列表中的removeall() [英] removeall() in list

查看:176
本文介绍了列表中的removeall()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

线程安全list.removeall(X)的任何想法:当L可能同时修改时,删除列表L中所有的&b
出现的事件?


此致,

Aaron

Any ideas for a thread-safe list.removeall( X ): removing all
occurrences of X within list L, when L might be modified concurrently?

Sincerely,
Aaron

推荐答案

ca ******** @ gmail.com 写道:

任何关于线程安全列表的想法.removeall(X):当L可能被同时修改时,删除列表L中所有的&b
次出现?
Any ideas for a thread-safe list.removeall( X ): removing all
occurrences of X within list L, when L might be modified concurrently?



那种方式就是疯狂。做一些明智的事情。在列表中放置一个锁定
,或者将列表中的所有mutator放入单个

线程中,或者其他什么。不要做你正在描述的事情。

That way lies madness. Do something sensible instead. Put a lock
around the list, or put all mutators for the list into a single
thread, or whatever. Don''t do what you''re describing.


1月11日下午2:57,Paul Rubin< http://phr...@NOSPAM .invalidwrote:
On Jan 11, 2:57 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:

castiro ... @ gmail.com写道:
castiro...@gmail.com writes:

线程的任何想法-safe list.removeall(X):当L可能同时被修改时,删除列表L中所有的&b
次出现?
Any ideas for a thread-safe list.removeall( X ): removing all
occurrences of X within list L, when L might be modified concurrently?



那种方式就是疯狂。做一些明智的事情。在列表中放置一个锁定
,或者将列表中的所有mutator放入单个

线程中,或者其他什么。不要做你在描述的事情。


That way lies madness. Do something sensible instead. Put a lock
around the list, or put all mutators for the list into a single
thread, or whatever. Don''t do what you''re describing.



这个函数只是希望X不在列表中。如果这个

发生在其他事情之前,期间或之后都无关紧要;只要它发生。

This function just wants X out of the list. It doesn''t matter if this
happens before, during, or after something else; so long as it happens.


ca ****** **@gmail.com 写道:

这个函数只是希望X不在列表中。如果这个

发生在其他事情之前,期间或之后都无关紧要;只要它发生。
This function just wants X out of the list. It doesn''t matter if this
happens before, during, or after something else; so long as it happens.



如果你以一般方式询问如何做这样的事情,

有几种基本方法:


1.让一个线程负责列表,并通过队列中的消息与它通信

。为了让X从列表中删除,你会向mutator线程发送一条要求删除的消息。 mutator

线程将循环读取并处理队列中的消息,

阻止没有请求待处理。这是一种首选的g />
Python样式并且非常简单,但是如果有很多这样的对象,你可能会得到比实际更多的线程

想要。


2.将锁定与列表相关联。任何想要访问

列表的东西都应该获得锁,做它的东西,然后释放锁。

一段时间后这会让人感到困惑。

3.各种其他方案涉及更细粒度的锁等

极易出错(竞争条件等)。


有可能一个关于使用

线程进行编程的好教程。它有时是一个棘手的主题,因此值得花时间研究各种方法,而不是重新发明轮辋。

If you''re asking in a general way how to do something like that,
there are several basic methods:

1. Put a single thread in charge of the list, and communicate with it
by message passing through Queues. To get X out of the list, you''d
send the mutator thread a message asking for removal. The mutator
thread would loop reading and processing messages from the queue,
blocking when no requests are pending. This is sort of the preferred
Python style and is pretty simple to get correct, but if there are
many such objects you can end up with more threads than you really
want.

2. Associate a lock with the list. Anything wanting to access the
list should acquire the lock, do its stuff, then release the lock.
This gets confusing after a while.

3. Various other schemes involving finer grained locks etc. that
get horrendously error prone (race conditions etc).

There is probably a good tutorial somewhere about programming with
threads. It''s sometimes a tricky subject, so it''s worth taking
the time to study various approaches rather than re-inventing the wheeel.


这篇关于列表中的removeall()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆