queue

Communicating end of Queue

允我心安 提交于 2019-12-19 05:33:05
问题 I'm learning to use the Queue module, and am a bit confused about how a queue consumer thread can be made to know that the queue is complete. Ideally I'd like to use get() from within the consumer thread and have it throw an exception if the queue has been marked "done". Is there a better way to communicate this than by appending a sentinel value to mark the last item in the queue? 回答1: original (most of this has changed; see updates below) Based on some of the suggestions (thanks!) of Glenn

is std::queue thread safe with producer and multiple consumers

萝らか妹 提交于 2019-12-19 05:15:30
问题 how can I make a queue thread safe? I need to push / pop / front / back and clear. is there something similar in boost? I have one producer and one or more consumer. 回答1: You must protect access to std::queue . If you are using boost protect it using boost::mutex . Now if you have multiple readers and one writer thread look at boost::shared_lock (for readers) and boost::unique_lock (for writer). However if you will encounter writer thread starvation look at boost::shared_mutex . 回答2: std:

limit size of Queue<T> in C++

人走茶凉 提交于 2019-12-19 05:06:58
问题 I notice the thread of similar question: Limit size of Queue<T> in .NET? That's exactly what I want to do, but I am not using .net but GNU C++. I have no reference to the base class in GNU C++, so java like super.***() or .net like base.***() will not work. I have been trying to inherit from queue class but it turns out in vain. What I want to do: specify the size of the queue, and automatically dequeue when the queue is full. To be specific: if the maximum size of my queue is 2, when I push

c# Adding a Remove(int index) method to the .NET Queue class

血红的双手。 提交于 2019-12-19 05:01:36
问题 I would like to use the generic queue class as described in the .NET framework (3.5) but I will need a Remove(int index) method to remove items from the queue. Can I achieve this functionality with an extension method? Anyone care to point me in the right direction? 回答1: What you want is a List<T> where you always call RemoveAt(0) when you want to get the item from the Queue . Everything else is the same, really (calling Add would add an item to the end of the Queue ). 回答2: Combining both

Cancel all AsyncTask?

回眸只為那壹抹淺笑 提交于 2019-12-19 03:27:25
问题 I have a class which is used to get media file thumbs. This Loader like class starts an AsyncTask for every ImageView (is called in SomeAdapter#getView() ). The task itself does a lot of things, and one of them is calling DiskLruCache , but when the SD card is unmounted, while the tasks are still running the application crashes. I know how to register if the card state is changed. So I need an approach how to stop all the running tasks. Any help would be nice. 回答1: just iterate through all

Python how to kill threads blocked on queue with signals?

泄露秘密 提交于 2019-12-19 03:10:56
问题 I start a bunch of threads working on a queue and I want to kill them when sending the SIGINT (Ctrl+C). What is the best way to handle this? targets = Queue.Queue() threads_num = 10 threads = [] for i in threads_num: t = MyThread() t.setDaemon(True) threads.append(t) t.start() targets.join() 回答1: Isn't Ctrl + C SIGINT ? Anyway, you can install a handler for the appropriate signal, and in the handler: set a global flag that instructs the workers to exit, and make sure they check it

Iterate through Queue of Objects in Order

只愿长相守 提交于 2019-12-18 19:44:09
问题 I have created a queue containing objects which I would like to iterate through in the order that they were placed within the queue (First object placed in queue, 2nd object placed in queue, 3rd object...) I saw a way of doing this online but I'm not sure if this will guarantee that objects in the queue will be visited in the correct order? for(MyObject anObject : queue){ //do someting to anObject... Thank you for your help. 回答1: It depends on which Queue implementation you use. For example

python threading Queue producer-consumer with thread-safe

本小妞迷上赌 提交于 2019-12-18 17:53:32
问题 I am using threading and Queue to fetch url and store to database. I just want one thread to do storing job. so I write code as below: import threading import time import Queue site_count = 10 fetch_thread_count = 2 site_queue = Queue.Queue() proxy_array=[] class FetchThread(threading.Thread): def __init__(self,site_queue,proxy_array): threading.Thread.__init__(self) self.site_queue = site_queue self.proxy_array = proxy_array def run(self): while True: index = self.site_queue.get() self.get

Is dispatch_sync(dispatch_get_global_queue(xxx), task) sync or async

风格不统一 提交于 2019-12-18 17:06:14
问题 As Apple's document says, dispatch_get_global_queue() is a concurrent queue, and dispatch_sync is something meaning serial.Then the tasks are processed async or sync? 回答1: You're getting confused between what a queue is and what async vs sync means. A queue is an entity on which blocks can be run. These can be serial or concurrent. Serial means that if you put block on in the order A, B, C, D, then they will be executed A, then B, then C, then D. Concurrent means that these same blocks might

Queuing promises

谁都会走 提交于 2019-12-18 13:29:12
问题 I use mbostock/queue for queuing few async operation. It is more to rate limit (UI generate few events, where the backend can process it slowly), and also to make sure they are processed sequentially. I use it like function request(d, cb) { //some async oper add.then(function(){ cb(null, "finished ") }) } var addQ = queue(1); addQ.defer(request) //called by few req at higher rates generated by UI I already uses angular.js $q for async operation. So, do I have to use mbostock/queue , or can I