locking

Any downsides to locking a collection vs. a syncRoot?

半世苍凉 提交于 2019-12-04 09:44:03
问题 I'm wondering if there are any downsides to locking over a collection such as a List<T> , HashSet<T> , or a Dictionary<TKey, TValue> rather than a simple object . Note: in the following examples, that is the only place where the locks occur, it's not being locked from multiple places, but the static method may be called from multiple threads. Also, the _dict is never accessed outside of the GetSomething method. My current code looks like this: private static readonly Dictionary<string, string

How do I use the LOCK ASM prefix to read a value?

狂风中的少年 提交于 2019-12-04 09:37:21
问题 I know how to use LOCK to thread-safely increment a value: lock inc [J]; But how do I read [J] (or any value) in a thread-safe manner? The LOCK prefix can't be used with mov. And if I do the following: xor eax, eax; lock add eax, [J]; mov [JC], eax; It raises an error on line 2. 回答1: Use XADD or MOV instruction instead ADD instruction! See also MFENCE, LFENCE and SFENCE instructions! EDIT: You can't use LOCK instruction with ADD instruction if source operand is a memory operand! From: "Intel®

why does this code lock my files?

左心房为你撑大大i 提交于 2019-12-04 08:37:54
Ive narrowed down to this method but i don't understand why its locking the file. I believe you could use something like using( something) { //do stuff here } But im not sure that would A) solve the issue or B) be the correct way if it did. any ideas? [DllImport("user32.dll", CharSet = CharSet.Auto)]private static extern Int32 SystemParametersInfo(UInt32 action, UInt32 uParam, String vParam, UInt32 winIni); private static readonly UInt32 SPI_SETDESKWALLPAPER = 0x14; private static readonly UInt32 SPIF_UPDATEINIFILE = 0x01; private static readonly UInt32 SPIF_SENDWININICHANGE = 0x02; private

Filter Lock Algorithm

会有一股神秘感。 提交于 2019-12-04 08:30:41
I'm busy looking at the filter lock algorithm for n-thread mutual exclusion and I can't seem to understand line 17 of the code. I understand that it is spinning on a condition but not entirely sure what those conditions are. More specifically what (∃k != me) entails. 1 class Filter implements Lock { 2 int[] level; 3 int[] victim; 4 public Filter(int n) { 5 level = new int[n]; 6 victim = new int[n]; // use 1..n-1 7 for (int i = 0; i < n; i++) { 8 level[i] = 0; 9 } 10 } 11 public void lock() { 12 int me = ThreadID.get(); 13 for (int i = 1; i < n; i++) { //attempt level 1 14 level[me] = i; 15

Do atomic operations become slower as more CPUs are added?

三世轮回 提交于 2019-12-04 07:59:08
问题 x86 and other architectures provide special atomic instructions (lock, cmpxchg, etc.) that allow you to write 'lock free' data structures. But as more and more cores are added, it seems as though the work these instructions will actually have to do behind the scenes will grow (at least to maintain cache coherency?). If an atomic add takes ~100 cycles today on a dual core system, might it take significantly longer on the 80+ core machines of the future? If you're writing code to last, might it

Java: What, if anything, is locked by synchronized methods apart from the object they belong to?

[亡魂溺海] 提交于 2019-12-04 07:38:18
Now, I'm not sure whether this is a stupid question, please bear with me if it is. Is the lock on an object "recursive", i. e. if two objects have references to a third object in their fields and a thread is running a synchronized method on one of the two, can any other thread access the third object? // a and b are some objects that implement Runnable // they both reference the same third object a.ref = c; b.ref = c; // a is run in a thread and processes some data in a loop for a long time // the method the loop belongs to is declared synchronized threadA = new Thread(a); threadA.start(); a

Object is currently in use elsewhere

强颜欢笑 提交于 2019-12-04 07:24:51
I'm getting this error, and it looks like it's because the same Bitmap object is being accessed by different threads. However I am using locks everywhere with it. public class MySingleInstanceClass { private Object locker = new Object(); private Bitmap myImage = new Bitmap(100, 100); public Bitmap MyImage { get { lock (locker) return myImage; } private set { lock (locker) myImage = value; } } private void Refresh() { lock (locker) { var g = Graphics.FromImage(myImage); // do more processing } } } Class MySingleInstanceClass will have only one instance. Calls to MyImage and Refresh() may come

How to lock with ReentrantLock?

喜夏-厌秋 提交于 2019-12-04 07:19:02
问题 I would expect the following test to only print "has been locked" once. BUT it consequently prints the line. public class LocKTest { @Test public void testLock() { Lock lock = new ReentrantLock(); while (true) { if (lock.tryLock()) { //lock.lock(); same result if I include an explicit lock here System.out.println("has been locked"); } } } } As far as I understood, tryLock will lock the ReentrantLock if possible (ie if not locked yet). But obviously this is not the case. How can I set such a

HttpURLConnection getting locked

半世苍凉 提交于 2019-12-04 06:51:02
I have a thread running under tomcat which creates a HttpUrlConnection and reads it through BufferedInputStream. After fetching data for some urls, it stalls. I got the jstack of the process which says HttpUrlConnection is locked and BufferedInputStream is also locked. "http-8080-1" daemon prio=10 tid=0x08683400 nid=0x79c9 runnable [0x8f618000] java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:129) at java.io.BufferedInputStream.fill(BufferedInputStream.java:218) at java.io.BufferedInputStream

Can Oracle's default object lock timeout be changed?

风流意气都作罢 提交于 2019-12-04 06:29:35
> ALTER PACKAGE blah COMPILE; (wait about 10 minutes) > ORA-04021: timeout occurred while waiting to lock object I understand why I get the timeout error (the package is in use). Does anyone know if there's a way to change the default 10 minute wait interval? Can this be configured at a database / session / statement level? Thanks there is a DDL_LOCK_TIMEOUT parameter since 11gR1 : DDL_LOCK_TIMEOUT specifies a time limit for how long DDL statements will wait in a DML lock queue. The default value of zero indicates a status of NOWAIT . The maximum value of 1,000,000 seconds will result in the