multithreading

How many is too many for create dispatch_queues in GCD (grand central dispatch)?

邮差的信 提交于 2020-05-25 16:20:50
问题 There is a wonderful article about a lightweight notification system built in Swift, by Mike Ash: (https://www.mikeash.com/pyblog/friday-qa-2015-01-23-lets-build-swift-notifications.html). The basic idea is you create objects that you can "listen" to i.e. invoke a callback on when there is some state change. To make it thread-safe, each object created holds its own dispatch_queue. The dispatch_queue is simply used to gate critical sections: dispatch_sync(self.myQueue) { // modify critical

How many is too many for create dispatch_queues in GCD (grand central dispatch)?

。_饼干妹妹 提交于 2020-05-25 16:19:27
问题 There is a wonderful article about a lightweight notification system built in Swift, by Mike Ash: (https://www.mikeash.com/pyblog/friday-qa-2015-01-23-lets-build-swift-notifications.html). The basic idea is you create objects that you can "listen" to i.e. invoke a callback on when there is some state change. To make it thread-safe, each object created holds its own dispatch_queue. The dispatch_queue is simply used to gate critical sections: dispatch_sync(self.myQueue) { // modify critical

parallelizing heterogenous tasks in R: foreach, doMC, doParallel

霸气de小男生 提交于 2020-05-25 08:27:39
问题 Here's what's been puzzling me: When you schedule a sequence of tasks that are homogenous in terms of content but heterogenous in terms of processing time (not known ex ante) using foreach, how exactly does foreach process these embarrassingly parallel tasks sequentially? For instance, I registered 4 threads registerDoMC(cores=4) and I have 10 tasks and the 4th and the 5th each turned out to be longer than all others combine. Then the first batch is obviously the 1st, 2nd, 3rd and 4th. When

Declaring an object as volatile

假装没事ソ 提交于 2020-05-24 22:40:30
问题 If you declare a member variable as volatile in Java, does this mean that all the object's data is stored in volatile memory, or that the reference to the object is stored in volatile memory? For example if I have the following class: class C{ int i = 0; char c = 'c'; } If I declare an instance of it as follows: private volatile C obj; does that store the reference to obj in volatile memory, or obj 's data ( obj.i and obj.c ) in volatile memory? Does it make obj.c and obj.i thread safe or not

How does ThreadPoolExecutor().map differ from ThreadPoolExecutor().submit?

老子叫甜甜 提交于 2020-05-24 08:53:28
问题 I was just very confused by some code that I wrote. I was surprised to discover that: with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: results = list(executor.map(f, iterable)) and with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: results = list(map(lambda x: executor.submit(f, x), iterable)) produce different results. The first one produces a list of whatever type f returns, the second produces a list of concurrent.futures.Future objects that then

Corrupted output with C++, cin, cout, threads and sync_with_stdio

梦想与她 提交于 2020-05-24 08:48:09
问题 I am trying to make a program in C++ to process a lot of packets in the fastest way possible. All the packets come from the standard should be read as fast as possible, sent to one thread from a pool to do the processing and then handled to an output thread that will write the packet to the standard output. When you are using the standard input and output in C++, it's recommended that before any input or output you call to the std::ios_base::sync_with_stdio(false) function. In some

Examples/Illustration of Wait-free And Lock-free Algorithms

99封情书 提交于 2020-05-24 08:45:47
问题 I've read that wait-free causes all threads to finish independently and lock-free ensures the program as a whole completes. I couldn't quite get it. Can anyone give an example (java) illustrating this. EDIT: Does lock-free mean a program without deadlock? 回答1: If a program is lock-free, it basically means that at least one of its threads is guaranteed to make progress over an arbitrary period of time. If a program deadlocks, none of its threads (and therefore the program as a whole) cannot

Is java ExecutorService newSingleThreadExecutor performs all the tasks using only one Thread?

╄→尐↘猪︶ㄣ 提交于 2020-05-24 05:24:49
问题 I know that a java thread cannot be restarted. So when I submit more than one tasks to newSingleThreadExecutor, then how does it perform all tasks using single thread? My understanding is that newSingleThreadExecutor will use maximum one thread at a time to process any submitted tasks. I guess same for newFixedThreadPool. If a Thread cannot be restarted then for performing n tasks, n threads should be spawned. I think newSingleThreadExecutor, newFixedThreadPool will make sure that not many

Parallel.ForEach not spawning all the threads

末鹿安然 提交于 2020-05-24 03:45:45
问题 I'm having some trouble using Parallel.ForEach. I need to simulate a couple of hardware components, that wait for an incoming connection, and reply to it. My current code is as follows: Task.Factory.StartNew(() => components, (component) => { var listener = new TcpListener(component.Ip, component.Port); while(true) { using(var socket = listener.AcceptSocket()) { //Read out socket and send a reply socket.Close(); } } }); The problem I'm having is: Not every component will get his own thread

Parallel.ForEach not spawning all the threads

牧云@^-^@ 提交于 2020-05-24 03:44:24
问题 I'm having some trouble using Parallel.ForEach. I need to simulate a couple of hardware components, that wait for an incoming connection, and reply to it. My current code is as follows: Task.Factory.StartNew(() => components, (component) => { var listener = new TcpListener(component.Ip, component.Port); while(true) { using(var socket = listener.AcceptSocket()) { //Read out socket and send a reply socket.Close(); } } }); The problem I'm having is: Not every component will get his own thread