callFromThread 是线程安全的
我查看了 callFromThread 的代码。它将所有可调用对象附加到 threadCallQueue 列表中。如果多个线程调用callFromThread,callFromThread怎么能是线程安全的呢?换句话说,如果没有threadCallQueue的锁,callFromThread怎么能是线程安全的呢?我错过了一些基本的东西吗?
I looked at the code for callFromThread. It's appending all the callables into threadCallQueue list. If multiple threads call callFromThread, how can callFromThread be thread-safe? In other words, without a lock on threadCallQueue, how can callFromThread be threadsafe? Am I missing something basic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多个线程可以将项目添加到列表中(这些是生产者),并且始终通过 append() 调用来完成(因此,在列表的末尾):
只有主线程会读取项目并从列表中删除它们(这个是消费者)并且它总是从列表的开头读取:
因此,由于一个线程从列表的开头读取而其他线程在末尾写入,所以这是线程安全的只要 Python 列表是。该函数的源代码中对此进行了注释:
Multiple threads can add items to the list (these are the producers) and it's always done through an append() call (so, at the end of the list):
Only the main thread ever reads items and removes them from the list (this one is the consumer) and it always reads at the beginning of the list:
So, since one thread reads from the beginning of the list and others write at the end, this is thread-safe as long as Python lists are. This is noted in the function's source code: