queue

Coalescing items in channel

≯℡__Kan透↙ 提交于 2019-12-11 04:05:00
问题 I have a function which receives tasks and puts them into a channel. Every task has ID, some properties and a channel where result will be placed. It looks like this task.Result = make(chan *TaskResult) queue <- task result := <-task.Result sendReponse(result) Another goroutine takes a task from the channel, processes it and puts the result into task's channel task := <-queue task.Result <- doExpensiveComputation(task) This code works fine. But now I want to coalesce tasks in the queue . Task

Java/Android: Synchronized vs Queue implementation

不想你离开。 提交于 2019-12-11 03:58:47
问题 Can I ask a rookie Java question? I am downloading some files from the web. This method download(url location) is called multiple times. public static void download(final String url) { Thread t = new Thread("download") { @Override public void run() { try { synchronized (object) { // download & save } } catch(Exception e) {} } }; t.start(); } I added "synchronized" so that downloads happens one-by-one. (not multiple downloads occur at the same time). I am guessing even though download() is

Is this the right way to compare two GCD Queues?

淺唱寂寞╮ 提交于 2019-12-11 03:58:14
问题 Following an earlier question on SO, I'm now looking to compare two different grand central dispatch queues to try and determine if the current code is being run on the main thread or not. My question simply: is this a valid way of achieving this? Or are there some pitfalls of doing this that I haven't considered? if (dispatch_get_current_queue() != dispatch_get_main_queue()) { // We are currently on a background queue } else { // We are on the main queue } Cheers 回答1: Comparing the current

std::queue initialization with NULL

元气小坏坏 提交于 2019-12-11 03:45:33
问题 Is it possible to initialize a C++ std::queue with a NULL value like other variables? Like this: HANDLE variable = NULL; class Test { } i.e. std::queue<Test*> testQueue = NULL; or testQueue.empty(); or something like that? 回答1: If you write this: std::queue<Test*> testQueue; //it is default initialized then that is enough; no need to make it pointer and initialized it with NULL. Also, you can do this: if ( testQueue.empty()) { //testQueue is empty } 回答2: If you use a pointer to a queue, you

how can i store array to queue in c++

你说的曾经没有我的故事 提交于 2019-12-11 03:12:41
问题 queue < int* > qq; for (int i = 0; i < N; i++) { int cc[2] = {i, i + 1}; qq.push(cc); } The N is large but not Exact so I want use queue. I want to store many arrays to queue,but the arrays which qq stored are the same one. How can I do it? 回答1: Your code won't work. Each cc has the same stack location in the loop. You need to allocate the cc array in the heap, perhaps using int *cc = new int[2]; (but then you need to delete it later). A better way would be to have cc declared as a std:

Implementing queue using stack

与世无争的帅哥 提交于 2019-12-11 03:10:01
问题 This is question from homework: Implement a FIFO queue using two stacks. The total running time of Enqueue and Dequeue functions should be O(n) in the worst case scenario. Also, analyze the running time of the algorithm. What I did: void Enqueue(T *value) { s1.Push(value); } T *Dequeue() { if (s2.size > 0) return s2.Pop(); else if (s1.size > 0) { for (int i = 0; i < s1.size; i++) s2.Push(s1.Pop()); return s2.Pop(); } else return NULL; } Analysis of the algorithm: Running time of one Enqueue

Help me understand this algorithm (simple)

大城市里の小女人 提交于 2019-12-11 02:58:52
问题 I have just made a queue class and I now have to use it to do this. Write a c++ program to generate all strings using A,B,and C as the letters. The strings must be generated in the following order: A B C AA AB AC BA BB BC CA CB CC AAA AAB AAC ABA ABB ABC ACA ACB ACC etc. It's supposed to do this until my queue overflows. Now, I simply don't understand the algorithm the teacher suggested using, which is this. Start with A and B and C in the queue. “Remove it Display it then Add Add Add ” The

Long running tasks in Pyramid web app

China☆狼群 提交于 2019-12-11 02:38:34
问题 I need to run some tasks in background of web app (checking the code out, etc) without blocking the views. The twist in typical Queue / Celery scenario is that I have to ensure that the tasks will complete, surviving even web app crash or restart until those tasks complete, whatever their final result. I was thinking about recording parameters for multiprocessing.Pool in a database and starting all the incomplete tasks at webapp restart. It's doable, but I'm wondering if there's a simpler or

TensorFlow: Reading images in queue without shuffling

人盡茶涼 提交于 2019-12-11 01:53:53
问题 I have a training set of 614 images which have already been shuffled. I want to read the images in order in batches of 5. Because my labels are arranged in the same order, any shuffling of the images when being read into the batch will result in incorrect labelling. These are my functions to read and add the images to the batch: # To add files from queue to a batch: def add_to_batch(image): print('Adding to batch') image_batch = tf.train.batch([image],batch_size=5,num_threads=1,capacity=614)

How to create exclusive queue consumer in Mule?

点点圈 提交于 2019-12-11 01:16:40
问题 In ActiveMQ you configure an exclusive consumer for a queue like: Queue_Name_Here?consumer.exclusive=true How to configure an exclusive consumer like above in Mule? 回答1: You need to URL encode the queue name, as Mule might try to decode the parameters as Mule transport options, which they are not. <jms:inbound-endpoint queue="Queue_Name_Here%3Fconsumer.exclusive%3Dtrue" connector-ref="Active_MQ" doc:name="JMS"/> 回答2: I was using a jms:activemq-xa-connector for distributed transactions and