multithreading

Explain how JIT reordering works

自古美人都是妖i 提交于 2020-05-23 21:33:10
问题 I have been reading a lot about synchronization in Java and all the problems that can occur. However, what I'm still slightly confused about is how the JIT can reorder a write. For instance, a simple double check lock makes sense to me: class Foo { private volatile Helper helper = null; // 1 public Helper getHelper() { // 2 if (helper == null) { // 3 synchronized(this) { // 4 if (helper == null) // 5 helper = new Helper(); // 6 } } return helper; } } We use volatile on line 1 to enforce a

How to create a Task which always yields?

ぐ巨炮叔叔 提交于 2020-05-23 13:24:43
问题 In contrast to Task.Wait() or Task.Result , await ’ing a Task in C# 5 prevents the thread which executes the wait from lying fallow. Instead, the method using the await keyword needs to be async so that the call of await just makes the method to return a new task which represents the execution of the async method. But when the await ’ed Task completes before the async method has received CPU time again, the await recognizes the Task as finished and thus the async method will return the Task

QObject::connect: Cannot queue arguments of type 'QTextCursor'

五迷三道 提交于 2020-05-23 07:20:21
问题 Im trying to send a signal from a non-main thread in PyQt but i dont know what am doing wrong! And when i execute the program it fails with this error: QObject::connect: Cannot queue arguments of type 'QTextCursor' (Make sure 'QTextCursor' is registered using qRegisterMetaType().) here is my code: class Sender(QtCore.QThread): def __init__(self,q): super(Sender,self).__init__() self.q=q def run(self): while True: pass try: line = q.get_nowait() # or q.get(timeout=.1) except Empty: pass else:

How to wait for signal in WinForms while also listening to events?

你。 提交于 2020-05-22 09:21:31
问题 Case 1 Here is my setup. internal class MyClass { private ApiObject apiObject; private bool cond1; private bool cond2; internal MyClass() { this.apiObject = new ApiObject(); this.apiObject.ApiStateUpdate += new ApiStateUpdateEventHandler(ApiStateHandler); //wait for both conditions to be true } private void ApiStateHandler(string who, int howMuch) { if(who.Equals("Something") && howMuch == 1) this.cond1 = true; else if(who.Equals("SomethingElse") && howMuch == 1) this.cond2 = true; } } How

How to wait for signal in WinForms while also listening to events?

人走茶凉 提交于 2020-05-22 09:20:27
问题 Case 1 Here is my setup. internal class MyClass { private ApiObject apiObject; private bool cond1; private bool cond2; internal MyClass() { this.apiObject = new ApiObject(); this.apiObject.ApiStateUpdate += new ApiStateUpdateEventHandler(ApiStateHandler); //wait for both conditions to be true } private void ApiStateHandler(string who, int howMuch) { if(who.Equals("Something") && howMuch == 1) this.cond1 = true; else if(who.Equals("SomethingElse") && howMuch == 1) this.cond2 = true; } } How

Is the DCL(double-checked locking) implemented in the following C ++ code thread-safe?

人走茶凉 提交于 2020-05-17 08:49:17
问题 Here is a piece of code that is DCL (double-checked locking) implemented by ‘acquire-release’ semantics in C++. The code is as follows: std :: atomic <Singleton *> Singleton :: m_instance; std :: mutex Singleton :: m_mutex; Singleton * Singleton :: getInstance () { Singleton * tmp = m_instance.load (std :: memory_order_acquire); // 3 if (tmp == nullptr) { std :: lock_guard <std :: mutex> lock (m_mutex); tmp = m_instance.load (std :: memory_order_relaxed); if (tmp == nullptr) { tmp = new

JavaFX: Can a node's tranforms be safely manipulated from a non-UI thread? [duplicate]

余生长醉 提交于 2020-05-17 06:25:28
问题 This question already has an answer here : Update JavaFX Live nodes outside Application Thread (1 answer) Closed 5 months ago . I've been working on a robot simulator using javafx. It uses a separate thread to calculate where the robot and each of its parts should be after each time increment. The actual updating of the UI is handled with a call to Platform.runLater() . Here is an example of how a node (in this case a Rectangle) called leftFinger would be manipulated: First, in the Controller

Deadlock in Parallel.Foreach while using ExecuteNonQuery?

梦想的初衷 提交于 2020-05-17 03:01:34
问题 I am facing deadlock error while using Parallel.Foreach. I have 1000 records in datatable and i'hv created 5 threads to process it. but when i'hv run this console application then after some records processed it will create a deadlock and no other records will process. Here is my code : Parallel.ForEach(dt1.AsEnumerable(), new ParallelOptions { MaxDegreeOfParallelism = 5 }, dr => { cmd1.CommandText = $"Update AuditMessage set Status=1" + $" where SXAEASCoreAuditMessageID ='{Convert.ToString

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) { } }

Are method References as method parameters thread safe in Java

*爱你&永不变心* 提交于 2020-05-16 08:58:07
问题 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) { } }