concurrency

Forked IORef reader function seems to stall main thread

最后都变了- 提交于 2019-12-23 12:24:09
问题 I was doing some experiments with concurrency and memory visibility and ran into this strange behavior (see comments inline): module Main where import Data.IORef import Control.Concurrent import System.CPUTime import System.IO main = do hSetBuffering stdout NoBuffering r <- newIORef False putStrLn "forking..." -- PRINTED forkIO $ f r threadDelay 1000000 putStrLn "writeIORef" -- NEVER PRINTED writeIORef r True threadDelay maxBound f :: IORef Bool -> IO () f r = readIORef r >>= \b-> if b then

Will a calling thread see modifications to local variables after thread.join()?

天大地大妈咪最大 提交于 2019-12-23 12:13:31
问题 In the simplest possible example, let's say I have a function that starts a thread, which in turn sets the value of a local variable to true. We join the thread, then leave the function. bool func() { bool b = false; std::thread t([&]() { b = true; }); t.join(); return b; } Will this function return true, or is the behavior undefined? 回答1: Yes, it must return true. [thread.thread.member] void join(); 4 Effects : Blocks until the thread represented by *this has completed. 5 Synchronization :

visibility of side effects when creating and joining threads

旧城冷巷雨未停 提交于 2019-12-23 12:07:34
问题 When are writes that are performed by one thread visible to a different thread when there are no synchronized blocks and no volatile variables? Here is a simplified quicksort example: int middle = partitionForTheFirstTime(array); Thread t = new Thread(new Quicksorter(array, 0, middle - 1)); Thread u = new Thread(new Quicksorter(array, middle + 1, array.size - 1)); t.start() u.start(); t.join(); u.join(); (For the sake of simplicity, assume that the two "worker threads" do not spawn any

JMM guarantees about final as field and non final reference to the object

假如想象 提交于 2019-12-23 12:07:25
问题 I try to understand final fields semantic. Lets research code: public class App { final int[] data; static App instance; public App() { this.data = new int[]{1, 0}; this.data[1] = 2; } public static void main(String[] args) { new Thread(new Runnable() { public void run() { instance = new App(); } }).start(); while (instance == null) {/*NOP*/} System.out.println(Arrays.toString(instance.data)); } } I have some questions: Does jmm guarantee, that if application terminates then it output [1,2] ?

Is volatile enough for changing reference to a list?

三世轮回 提交于 2019-12-23 10:19:35
问题 Let's say we have a list reference: volatile List<Object> a; now thread 1 initializes it: List<Object> newA = new LinkedList<>(); newA.add(new String("a")); a = newA; // Write to a volatile (equivalent to exiting a synchronized block in terms of memory barriers) then thread 2 does: Object o = a.get(0); // Compound operation - first we read a volatile reference value, then invoke .get() method on it. Read to a volatile is equivalent to entering a synchronized block. Is "o" guaranteed to refer

Why don't WinForms/WPF controls use Invoke internally?

早过忘川 提交于 2019-12-23 10:17:51
问题 I understand why GUI controls have thread affinity. But why don't the controls use the invoke internally in their methods and properties? Now you have to do stuff like this just to update TextBox value: this.Invoke(new MethodInvoker(delegate() { textBox.Text = "newValue"; } While using just textBox.Text = "newValue"; would be enough to represent the same logic. All that would have to be done is change textBox.Text logic from this (pseudocode): public string Text { set { if(!this

Memcached – Are GET and SET operations atomic?

自作多情 提交于 2019-12-23 10:12:49
问题 Here is the scenario: a simple website which queries a memcached cache. That same cache is updated by a batch job every 10-15 minutes. With that pattern is there anything that could go wrong (e.g. cache miss)? I am concerned by all the possible racing condition that could happen. For example, if the website does a GET operation on an object cached in memcached while that same object is overridden by the batch job, what will happen? 回答1: My initial instinct was that you should be able to read

Is variable assignment atomic in go?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:33:23
问题 If I have two threads concurrently modifying a string field on a struct, will I always see one or the other string assigned to the field, but nothing else? 回答1: No. If you need atomic operations, there is sync/atomic. The Go Memory Model will have all the related details. From the top of the Memory Model document: Programs that modify data being simultaneously accessed by multiple goroutines must serialize such access. To serialize access, protect the data with channel operations or other

Mystery (concurrency/component drawing?) bug in very simple Swing dice program

丶灬走出姿态 提交于 2019-12-23 09:33:17
问题 Sorry for the poor question title, I'm stumped as to the cause of this bug and didn't know how to phrase the question. I'm learning basic Swing and doing this exercise from the online book Introduction to Programming with Java. I haven't followed the instructions to the letter and instead am trying to do this: have a window that shows a visual representation of two dice when you click on one of the dice, it 'rolls' and shows the new value My implementation: a very basic JDie object that

Does python have compare and swap operations

亡梦爱人 提交于 2019-12-23 09:28:04
问题 Trying to find if the python support CAS operations, lock free programming, concurrency like in java? 回答1: Python does not have those operations. Java has more sophisticated concurrency controls than Python does. CPython (the typical implementation almost everyone uses) has a Global Interpreter Lock that you will want to understand. Jython is a Python implementation on the JVM, and so shares many of the concurrency characteristics of Java. 来源: https://stackoverflow.com/questions/25219326/does