concurrency

Qt: How to lock/prevent a file from being read while it is written?

十年热恋 提交于 2021-01-21 10:28:39
问题 I am using Qt5 on Windows 7. In my current project I open a binary file in order to populate it with data coming from a TCP socket. Normally, after the file is populated, I close it and another application will read this binary file for further processing. Well, the problem is: The writing operation takes about 4-5 seconds (or even more) so I need to find a way to prevent the other application from reading from the binary file until the file is completely populated ... Here below is the code

Java: Does LinkedBlockingQueue take into account order of consumers?

巧了我就是萌 提交于 2021-01-21 07:40:21
问题 I have 3 threads: 2 consumers, ConsumerA and ConsumerB , and a Producer . I also have a LinkedBlockingQueue queue At t=1: ConsumerA calls queue.take() At t=2: Consumer B calls queue.take() At t=3: Producer calls queue.put(foo) Is it guaranteed that ConsumerA receives foo before ConsumerB? In other words, the order in which the consumers invokes take() is the order in which each is notified? If not, is there an alternative data structure that will give higher priority based on order? 回答1: From

Execution Context and Dispatcher - Best practices, useful configurations and Documentation

半腔热情 提交于 2021-01-20 14:29:10
问题 Scala Execution Context and Dispatchers - Listing and comparison: Why ? There are a lot of questions around what/how/what is the best Execution Context to use to execute futures on in Scala and how to configure the dispatcher. Still I never was able to find a longer list with pros and cons and configuration examples. The best I could find was in the Akka Documentation: http://doc.akka.io/docs/akka/snapshot/scala/dispatchers.html and Play Documentation https://www.playframework.com

Do Atomic variables guarantee a - “happens before relationship”?

夙愿已清 提交于 2021-01-20 12:43:13
问题 I have a requirement where I need to publish results of 'n' threads upon their completion. For checking if all the threads completed, I am using an AtomicInteger (incrementAndGet()) and comparing its value against a final variable. Before doing the check, I'm writing the results of individual threads to a shared object (into a concurrent-hashmap, as non-synchronized data structures dint seem to be adequate). So, my question is will all the threads complete writing to the shared object (and

Stop Reentrancy on MemoryCache Calls

你离开我真会死。 提交于 2021-01-20 04:13:47
问题 The app needs to load data and cache it for a period of time. I would expect that if multiple parts of the app want to access the same cache key at the same time, the cache should be smart enough to only load the data once and return the result of that call to all callers. However, MemoryCache is not doing this. If you hit the cache in parallel (which often happens in the app) it creates a task for each attempt to get the cache value. I thought that this code would achieve the desired result,

Why Java throw java.lang.IllegalMonitorStateException when I invoke wait() in static way synchronized block?

谁说胖子不能爱 提交于 2021-01-18 10:54:32
问题 I do not understand why Java throw exception from subject in this code. Could somebody explain me it? class Wait implements Runnable { public void run() { synchronized (Object.class) { try { while(true) { System.out.println("Before wait()"); wait(); System.out.println("After wait()"); } } catch (InterruptedException e) { e.printStackTrace(); } } } } public class ObjectMethodInConcurency { public static void main(String[] args) { Wait w = new Wait(); (new Thread(w)).start(); } } 回答1: Use

Python multiprocessing blocks indefinately in waiter.acquire()

淺唱寂寞╮ 提交于 2021-01-07 02:48:45
问题 Can someone explain why this code blocks and cannot complete? I've followed a couple of examples for multiprocessing and I've writting some very similar code that does not get blocked. But, obviously, I cannot see what is the difference between that working code and that below. Everything sets up fine, I think. It gets all the way to .get(), but none of the processes ever finish. The problem is that python3 blocks indefinitely in waiter.acquire(), which you can tell by interrupting it and

Read from n pipes from one process in parallel

三世轮回 提交于 2021-01-07 02:30:38
问题 I faced a concurrency problem when writing to the same named pipe created with mkfifo by multiple processes at the same time, where some writes got lost. Since the number of writing processes are limited I want to switch from "writing to 1 pipe from n processes and reading from 1 separate" to "writing to n pipes by n processes and reading from 1 separate process". Currently I'm reading via read line <"$pipe" in a loop until a condition is met. read blocks here until a line was read. How can I

Read from n pipes from one process in parallel

折月煮酒 提交于 2021-01-07 02:28:30
问题 I faced a concurrency problem when writing to the same named pipe created with mkfifo by multiple processes at the same time, where some writes got lost. Since the number of writing processes are limited I want to switch from "writing to 1 pipe from n processes and reading from 1 separate" to "writing to n pipes by n processes and reading from 1 separate process". Currently I'm reading via read line <"$pipe" in a loop until a condition is met. read blocks here until a line was read. How can I

Timeout while waiting for a batch of Futures to complete?

纵然是瞬间 提交于 2020-12-30 06:50:27
问题 I have a set of Futures created by submitting Callable s to an Executor . Pseudo code: for all tasks futures.add(executor.submit(new callable(task))) Now I'd like to get all futures waiting at most n seconds until all complete. I know I can call Future#get(timeout) but if I call that sequentially for all my futures in a loop the timouts start adding up. Pseudo code: for all futures future.get(timeout) get blocks with a timeout until the result is ready. Therefore, if the first completes just