synchronization

Java double checked locking

老子叫甜甜 提交于 2019-11-26 11:47:36
I happened upon an article recently discussing the double checked locking pattern in Java and its pitfalls and now I'm wondering if a variant of that pattern that I've been using for years now is subject to any issues. I've looked at many posts and articles on the subject and understand the potential issues with getting a reference to a partially constructed object, and as far as I can tell, I don't think my implementation is subject to these issues. Are there any issues with the following pattern? And, if not, why don't people use it? I've never seen it recommended in any of the discussion I

How do I make my ArrayList Thread-Safe? Another approach to problem in Java?

六眼飞鱼酱① 提交于 2019-11-26 11:45:31
I have an ArrayList that I want to use to hold RaceCar objects that extend the Thread class as soon as they are finished executing. A class, called Race, handles this ArrayList using a callback method that the RaceCar object calls when it is finished executing. The callback method, addFinisher(RaceCar finisher), adds the RaceCar object to the ArrayList. This is supposed to give the order in which the Threads finish executing. I know that ArrayList isn't synchronized and thus isn't thread-safe. I tried using the Collections.synchronizedCollection(c Collection) method by passing in a new

Are C++ Reads and Writes of an int Atomic?

我与影子孤独终老i 提交于 2019-11-26 11:44:21
I have two threads, one updating an int and one reading it. This is a statistic value where the order of the reads and writes is irrelevant. My question is, do I need to synchronize access to this multi-byte value anyway? Or, put another way, can part of the write be complete and get interrupted, and then the read happen. For example, think of a value = 0x0000FFFF that gets incremented value of 0x00010000. Is there a time where the value looks like 0x0001FFFF that I should be worried about? Certainly the larger the type, the more possible something like this to happen. I've always synchronized

How to synchronize a static variable among threads running different instances of a class in Java?

房东的猫 提交于 2019-11-26 11:32:59
I know that using the synchronize keyword before a method brings synchronization to that object. That is, 2 threads running the same instance of the object will be synchronized. However, since the synchronization is at the object level, 2 threads running different instances of the object will not be synchronized. If we have a static variable in a Java class that is called by the method, we would like it to be synchronized across instances of the class. The two instances are running in 2 different threads. Can we achieve synchronization in the following way? public class Test { private static

Using string as a lock to do thread synchronization

瘦欲@ 提交于 2019-11-26 11:28:52
问题 While i was looking at some legacy application code i noticed it is using a string object to do thread synchronization. I\'m trying to resolve some thread contention issues in this program and was wondering if this could lead so some strange situations. Any thoughts ? private static string mutex= \"ABC\"; internal static void Foo(Rpc rpc) { lock (mutex) { //do something } } 回答1: Strings like that (from the code) could be "interned". This means all instances of "ABC" point to the same object.

Mutex example / tutorial?

旧时模样 提交于 2019-11-26 11:27:03
I'm new to multithreading, and was trying to understand how mutexes work. Did a lot of Googling and I found a decent tutorial , but it still left some doubts of how it works because I created my own program in which locking didn't work. One absolutely non-intuitive syntax of the mutex is pthread_mutex_lock( &mutex1 ); , where it looks like the mutex is being locked, when what I really want to lock is some other variable. Does this syntax mean that locking a mutex locks a region of code until the mutex is unlocked? Then how do threads know that the region is locked? [ UPDATE: Threads know that

Is changing a pointer considered an atomic action in C?

人盡茶涼 提交于 2019-11-26 11:20:33
问题 If I have a multi-threaded program that reads a cache-type memory by reference. Can I change this pointer by the main thread without risking any of the other threads reading unexpected values. As I see it, if the change is atomic the other threads will either read the older value or the newer value; never random memory (or null pointers), right? I am aware that I should probably use synchronisation methods anyway, but I\'m still curious. Are pointer changes atomic? Update: My platform is 64

Is there a synchronization class that guarantee FIFO order in C#?

让人想犯罪 __ 提交于 2019-11-26 11:10:21
问题 What is it and how to use? I need that as I have a timer that inserts into DB every second, and I have a shared resource between timer handler and the main thread. I want to gurantee that if the timer handler takes more than one second in the insertion the waited threads should be executed in order. This is a sample code for my timer handler private void InsertBasicVaraibles(object param) { try { DataTablesMutex.WaitOne();//mutex for my shared resources //insert into DB } catch (Exception ex)

How to make a multiple-read/single-write lock from more basic synchronization primitives?

旧时模样 提交于 2019-11-26 10:15:56
问题 We have found that we have several spots in our code where concurrent reads of data protected by a mutex are rather common, while writes are rare. Our measurements seem to say that using a simple mutex seriously hinders the performance of the code reading that data. So what we would need is a multiple-read/single-write mutex. I know that this can be built atop of simpler primitives, but before I try myself at this, I\'d rather ask for existing knowledge: What is an approved way to build a

How can two instances of a userscript communicate between frames?

强颜欢笑 提交于 2019-11-26 09:46:17
问题 Refer to the technique of having the same JavaScript to run in both a web page and an iframe, as described in this answer: For example, suppose you have this page at domain_A.com: <html> <body> <iframe src=\"http://domain_B.com/SomePage.htm\"></iframe> </body> </html> If you set your @match directives like this: // @match http://domain_A.com/* // @match http://domain_B.com/* Then your script will run twice -- once on the main page and once on the iframe as though it were a standalone page.