thread-safety

Are you there, asynchronously written value?

时光毁灭记忆、已成空白 提交于 2020-07-03 06:25:33
问题 The last couple of days I've been reading about async/await. Yesterday I found this video on Channel 9 what made wonder about some things. Please consider the slide below. Aside from the issues Lucian Wischik addresses, I wondered about variable assignment. Say we changed the async void into async Task , and added await before the SendData call. This enables us to get the stream, assign the variable m_GetResponse , wait for two seconds and print it. But what happens to the variable? It could

Is tkinter's `after` method thread-safe?

让人想犯罪 __ 提交于 2020-06-27 20:04:28
问题 Since tkinter isn't thread-safe, I often see people use the after method to queue some code for execution in the main thread. Here's an example: import tkinter as tk from threading import Thread def change_title(): root.after(0, root.title, 'foo') root = tk.Tk() Thread(name='worker', target=change_title).start() root.mainloop() So instead of executing root.title('foo') directly in the worker thread, we queue it with root.after and let the main thread execute it. But isn't calling root.after

How do I atomically move a 64bit value in x86 ASM?

雨燕双飞 提交于 2020-06-27 12:59:47
问题 First, I found this question: How do I atomically read a value in x86 ASM? But its a bit different, in my case I want to atomically assign a float (64bit double) value in a 32bit application. From: "Intel® 64 and IA-32 ArchitecturesSoftware Developer’s Manual, Volume3A" The Pentium processor (and newer processors since) guarantees that the following additional memory operations will always be carried out atomically: Reading or writing a quadword aligned on a 64-bit boundary Is it actually

Scala Iterator for multithreading

余生颓废 提交于 2020-06-16 17:01:19
问题 I am using scala Iterator for waiting loop in synchronized block: anObject.synchronized { if (Try(anObject.foo()).isFailure) { Iterator.continually { anObject.wait() Try(anObject.foo()) }.dropWhile(_.isFailure).next() } anObject.notifyAll() } Is it acceptable to use Iterator with concurrency and multithreading? If not, why? And then what to use and how? There are some details, if it matters. anObject is a mutable queue. And there are multiple producers and consumers to the queue. So the block

C++ thread-safe uniform distribution random number generation

大城市里の小女人 提交于 2020-06-11 03:58:46
问题 I have a loop. Inside the loop, in each iteration, I need to draw a number from U[0,1]. How can I use openmp, and also make sure that the random number generating process is not contaminated? I got suggestion that I need a thread-safe random number generator, which may or may not be the solution to my problem. My question is very related to another one, with a slight difference that I want to draw from a coninuum U[0,1]. Additionally, I don't know how to seed generator by thread, can someone

Is it safe to finish an android activity from a background thread?

放肆的年华 提交于 2020-06-10 09:24:09
问题 In Android, is it safe to call Activity.finish() from a background thread, or can it only called from the main thread? The documentation doesn't mention anything about thread safety of this method. 回答1: No, it is not. The code uses at least one variable, mFinished, without synchronization. Full stop. public void finish() { if (mParent == null) { int resultCode; Intent resultData; synchronized (this) { resultCode = mResultCode; resultData = mResultData; } if (false) Log.v(TAG, "Finishing self:

Is a C# 7 tuple-based variable swap thread safe?

你离开我真会死。 提交于 2020-05-30 07:38:46
问题 Before C# 7's tuples, the standard way to swap two variables was something like: var foo = 5; var bar = 10; var temp = foo; foo = bar; bar = temp; But now we can use (foo, bar) = (bar, foo); It's on one line and it's prettier. But is it thread safe - is the swap done atomically, or is this just sugar over top of a multi-step operation? 回答1: "No, basically". The ValueTuple<...> family are mutable value types, which makes it complex. The older Tuple<...> family were immutable reference types;

Using C++11 thread_local with other parallel libraries

[亡魂溺海] 提交于 2020-05-29 02:43:46
问题 I have a simple question, can C++11 thread_local be used with other parallel models. For example, can I use it within a function while using OpenMP or Intel TBB to parallel the tasks. Most such parallel programming models hide hardware threads behind higher level API. My instinct is that they all have to map their task schedulers into hardware threads. Can I expect that C++11 thread_local will have expected effect. A simple example is, void func () { static thread_local some_var = init_val;

Java, visibility and infinite loop occurrence

谁都会走 提交于 2020-05-23 16:03:28
问题 I'm studying Java Concurrency in Practice and there it is explained why the following snippet of code is bad: public class NoVisibility { private static boolean ready; private static int number; private static class ReaderThread extends Thread { public void run() { while (!ready) { Thread.yield(); System.out.println(number); } } public static void main(String[] args) { new ReaderThread().start(); number = 42; ready = true; } } This code may print 0 or loop forever. While it is easy to

Are method References as method parameters thread safe in Java

心不动则不痛 提交于 2020-05-16 08:58:08
问题 i have the following scenario: interface ValueBinding<T> { public void setValue(T input); } public enum FacesBinding { VALUE; public void bindString(ValueBinding<String> fcn, HttpServletRequest req, String param){ try { String val = req.getParameter(param); if( val != null ) fcn.setValue(val); } catch (Exception e) { } } public void bindBoolean(ValueBinding<Boolean> fcn, HttpServletRequest req, String param){ try { fcn.setValue(req.getParameter(param) != null); } catch (Exception e) { } }