函数和数据指针如何允许c中的临时运行时多态性?(How function and data pointers permit ad hoc run-time polymorphism in c?)
关于C的维基百科文章指出,
Function and data pointers permit ad hoc run-time polymorphism
。请解释一下这个是什么意思?
wikipedia article on C states that
Function and data pointers permit ad hoc run-time polymorphism
.what is the meaning of this?please explain.
原文:https://stackoverflow.com/questions/11733169
最满意答案
(1)不,消费者没有锁; 它只是在请求满足之前被阻塞,但队列仍然可用。 如果有多个消费者,那么这个消费者就是潜在的收件人; 解析函数将选择满意的顺序。
(2)任何性能差异取决于实施。 不要问我们 - 你有最重要的权力在你面前:你的电脑。 使用您选择的测试场景和
timeit
工具。(1) No, the consumer is not holding a lock; it's merely blocked until the request is satisfied, but the queue is still available. If there are multiple consumers, then this one is in the pool of potential recipients; the resolution function will choose the order of satisfaction.
(2) Any performance difference depends on the implementation. Don't ask us -- you have the foremost authority in front of you: your computer. Use a test scenario of your choice, and the
timeit
facility.
相关问答
更多-
Queue.Queue和collections.deque具有不同的用途。 Queue.Queue旨在允许不同的线程使用排队的消息/数据进行通信,而collections.deque只是用作数据结构。 这就是为什么Queue.Queue具有put_nowait() , get_nowait()和join() ,而collections.deque则没有。 Queue.Queue不是用作集合的,这就是为什么它缺少in操作符。 它归结为:如果你有多个线程,并希望他们能够通信而不需要锁定,那么你正在寻找Queue ...
-
如果你想知道你的所有线程是否都在工作,你可以在每次启动任务时打印线程名称: from threading import Thread from queue import Queue import random import time class DownloadWorker(Thread): def __init__(self, queue): Thread.__init__(self) self.queue = queue def run(self) ...
-
(1)不,消费者没有锁; 它只是在请求满足之前被阻塞,但队列仍然可用。 如果有多个消费者,那么这个消费者就是潜在的收件人; 解析函数将选择满意的顺序。 (2)任何性能差异取决于实施。 不要问我们 - 你有最重要的权力在你面前:你的电脑。 使用您选择的测试场景和timeit工具。 (1) No, the consumer is not holding a lock; it's merely blocked until the request is satisfied, but the queue is sti ...
-
我想通了......毕竟答案在文档中: https://docs.python.org/3/library/multiprocessing.html#pipes-and-queues 尤其是: 它们的不同之处在于Queue缺少在Python 2.5的queue.Queue类中引入的task_done()和join()方法。 在我的情况下,解决方案似乎是将此添加到关闭代码: self.imgbuffer.cancel_join_thread() I figured it out... the answer ...
-
虽然其他人不可能运行此代码(它不是自包含的),但是你所展示的内容没有明显的问题。 所以问题在于你没有展示的东西 - 可能在代码创建和使用TaskExecutor实例。 当我插入缺少的部分时,我凭空捏造,这段代码运行良好。 所以你需要展示的不仅仅是这个。 如何更换: logger.debug("try to get queued task") 同 logger.debug("try to get queued task from queue %s", self.taskInfos) ? 那么至少我们可以看 ...
-
Queue.Queue与多线程Python代码中的信号量,锁等(Queue.Queue vs semaphores, locks, etc. in multithreaded Python code)[2023-07-18]
正如你的建议,我不确定我会考虑信号量并锁定“更强大的方法”。 队列通常是高阶抽象。 换句话说,您可以使用信号量和锁来构建线程安全的队列。 你在哪里使用取决于你的应用程序。 队列适用于在线程和进程之间传递“工作”,而信号量/锁定对于保护关键部分或共享资源很有用,因此一次只能访问一个线程。 I'm not sure I'd consider semaphores and locks "more powerful methods", as you suggest. Queues are generally a h ... -
python的queue.Queue.put()方法是异步的吗?(Is python's queue.Queue.put() method asynchronous? [duplicate])[2022-12-30]
我能解释那个吗? 您会观察到这种行为,因为您始终在修改同一个对象 让我们放置队列/线程,并使用一些prints运行简化的代码,以了解正在发生的事情 t = {} l = [] for i in range(3): t['a'] = i l.append(t) print(l) t['a'] = 20 print(l) print(map(id, l)) [{'a': 2}, {'a': 2}, {'a': 2}] [{'a': 20}, {'a': 20}, {'a': 20}] # th ... -
您的代码中存在竞争条件。 想象一下,在Queue只剩下一个项目,而你只使用两个线程而不是8.然后发生以下事件序列: 线程A调用q.empty来检查它是否为空。 由于队列中有一个项目结果为False ,因此执行循环体。 在线程A调用q.get之前,有一个上下文切换,线程B可以运行。 线程B调用q.empty ,队列中仍有一个项目因此结果为False并执行循环体。 线程B在没有参数的情况下调用q.get ,它立即返回队列中的最后一项。 然后线程B处理该项并退出,因为q.empty返回True 。 线程A开始运行 ...
-
我建议用collections.deque替换Queue.Queue使用。 Queue类旨在用于线程之间的同步通信,因此在用作常规数据结构时会产生一些不必要的开销。 collections.deque是一个更快的选择。 (名称“deque”的发音为“deck”,意思是“双端队列”。) deque类的确有与Queue类型不同的API,但它们之间的转换应该很容易。 使用deque.append代替Queue.put和deque.popleft代替q.get() (或者appendleft和pop ,如果你想转向 ...
-
我想你想从多处理模块中使用Queue ,然后以这种方式导入 - from multiprocessing import Queue 如果要使用队列数据结构,则使用小写queue 。 import queue I guess you want to use Queue from multiprocessing module, then import this way - from multiprocessing import Queue If you want to use the queue data ...