synchronization

Is volatile read happens-before volatile write?

你离开我真会死。 提交于 2019-11-27 15:04:12
问题 I try to understand why this example is a correctly synchronized program: a - volatile Thread1: x=a Thread2: a=5 Because there are conflicting accesses (there is a write to and read of a) so in every sequential consistency execution must be happens-before relation between that accesses. Suppose one of sequential execution: 1. x=a 2. a=5 Is 1 happens-before 2, why? 回答1: No, a volatile read before (in synchronization order) a volatile write of the same variable does not necessarily happens

Error:resource style/TextAppearance.Compat.Notification.Info (aka {packageId}.test:style/TextAppearance.Compat.Notification.Info) not found

无人久伴 提交于 2019-11-27 15:04:11
I just updated the build.gradle compile SDK to 27 API. compileSdkVersion 27 buildToolsVersion '27.0.3' targetSdkVersion 27 but but once I hit sync button it throws error: resource style/TextAppearance.Compat.Notification.Info (aka {packageId}.test:style/TextAppearance.Compat.Notification.Info) not found. error: resource style/TextAppearance.Compat.Notification.Info (aka {packageId}.test:style/TextAppearance.Compat.Notification.Info) not found. error: resource style/TextAppearance.Compat.Notification.Info (aka {packageId}.test:style/TextAppearance.Compat.Notification.Info) not found. error:

Close multiple goroutine if an error occurs in one in go

北城余情 提交于 2019-11-27 14:47:07
consider this function : func doAllWork() error { var wg sync.WaitGroup wg.Add(3) for i := 0; i < 2; i++ { go func() { defer wg.Done() for j := 0; j < 10; j++ { result, err := work(j) if err != nil { // can't use `return err` here // what sould I put instead ? os.Exit(0) } } }() } wg.Wait() return nil } In each goroutine, the function work() is called 10 times. If one call to work() returns an error in any of the running goroutines, I want all the goroutines to stop immediately, and the program to exit. Is it ok to use os.Exit() here ? How should I handle this ? Edit : this question is

Is there a C# equivalent to Java's CountDownLatch?

牧云@^-^@ 提交于 2019-11-27 14:32:08
问题 Is there a C# equivalent to Java's CountDownLatch? 回答1: The .NET Framework version 4 includes the new System.Threading.CountdownEvent class. 回答2: Here is a simple implementation (from 9 Reusable Parallel Data Structures and Algorithms): To build a countdown latch, you just initialize its counter to n, and have each subservient task atomically decrement it by one when it finishes, for example by surrounding the decrement operation with a lock or with a call to Interlocked.Decrement. Then,

Is it safe for more than one goroutine to print to stdout?

拈花ヽ惹草 提交于 2019-11-27 14:31:21
问题 I have multiple goroutines in my program, each of which makes calls to fmt.Println without any explicit synchronization. Is this safe (i.e., will each line appear separately without data corruption), or do I need to create another goroutine with synchronization specifically to handle printing? 回答1: No it's not safe even though you may not sometimes observe any troubles. IIRC, the fmt package tries to be on the safe side, so probably intermixing of some sort may occur but no process crash,

Can an Android AsyncTask doInBackground be synchronized to serialize the task execution?

吃可爱长大的小学妹 提交于 2019-11-27 14:30:34
Is it possible to make AsyncTask.doInBackground synchronized - or achieve the same result in another way? class SynchronizedTask extends AsyncTask { @Override protected synchronized Integer doInBackground(Object... params) { // do something that needs to be completed // before another doInBackground can be called } } In my case, any AsyncTask.execute() can be started before a previous one has completed, but I need to execute the code in doInBackground only after the previous task has finished. EDIT : As correctly pointed out, the synchronization works only on the same object instance.

What is the purpose of using synchronized (Thread.currentThread()){…} in Java?

安稳与你 提交于 2019-11-27 14:27:56
问题 I faced the following code in our project: synchronized (Thread.currentThread()){ //some code } I don't understand the reason to use synchronized on currentThread . Is there any difference between synchronized (Thread.currentThread()){ //some code } and just //some code Can you provide an example which shows the difference? UPDATE more in details this code as follows: synchronized (Thread.currentThread()) { Thread.currentThread().wait(timeInterval); } It looks like just Thread.sleep

Why is double-checked locking broken in Java?

痴心易碎 提交于 2019-11-27 14:22:59
This question relates to behaviour of old Java versions and old implementations of the double checked locking algorithm Newer implementations use volatile and rely on slightly changed volatile semantics, so they are not broken. It's stated that fields assignment is always atomic except for fields of long or double. But, when I read an explaination of why double-check locking is broken, it's said that the problem is in assignment operation: // Broken multithreaded version // "Double-Checked Locking" idiom class Foo { private Helper helper = null; public Helper getHelper() { if (helper == null)

Linux synchronization with FIFO waiting queue

拟墨画扇 提交于 2019-11-27 14:13:54
问题 Are there locks in Linux where the waiting queue is FIFO? This seems like such an obvious thing, and yet I just discovered that pthread mutexes aren't FIFO, and semaphores apparently aren't FIFO either (I'm working on kernel 2.4 (homework))... Does Linux have a lock with FIFO waiting queue, or is there an easy way to make one with existing mechanisms? 回答1: Here is a way to create a simple queueing "ticket lock", built on pthreads primitives. It should give you some ideas: #include <pthread.h>

increment a count value outside parallel.foreach scope

别说谁变了你拦得住时间么 提交于 2019-11-27 14:00:34
How can I increment an integer value outside the scope of a parallel.foreach loop? What's is the lightest way to synchronize access to objects outside parallel loops? var count = 0; Parallel.ForEach(collection, item => { action(item); // increment count?? } I like to beat dead horses! :) The "lightest" way to increment the count from multiple threads is: Interlocked.Increment(ref count); But as others have pointed out: if you're doing it inside Parallel.ForEach then you're probably doing something wrong. I'm suspecting that for some reason you're using ForEach but you need an index to the item