Questions tagged [locking]

Locking allows different types of resources to be used exclusively by one process at a time.

For more information see Locks in Java

8782 questions
43
votes
8 answers

How to solve the "Double-Checked Locking is Broken" Declaration in Java?

I want to implement lazy initialization for multithreading in Java. I have some code of the sort: class Foo { private Helper helper = null; public Helper getHelper() { if (helper == null) { Helper h; …
monsieurBelbo
  • 727
  • 1
  • 8
  • 16
43
votes
1 answer

Understanding Multiprocessing: Shared Memory Management, Locks and Queues in Python

Multiprocessing is a powerful tool in python, and I want to understand it more in depth. I want to know when to use regular Locks and Queues and when to use a multiprocessing Manager to share these among all processes. I came up with the following…
SmCaterpillar
  • 6,683
  • 7
  • 42
  • 70
43
votes
10 answers

Spinlocks, How Useful Are They?

How often do you find yourself actually using spinlocks in your code? How common is it to come across a situation where using a busy loop actually outperforms the usage of locks? Personally, when I write some sort of code that requires thread…
unknown
42
votes
5 answers

Why doesn't C# allow a null value to be locked?

C# doesn't allow locking on a null value. I suppose I could check whether the value is null or not before I lock it, but because I haven't locked it another thread could come along and make the value null! How can I avoid this race condition?
Casebash
  • 114,675
  • 90
  • 247
  • 350
42
votes
10 answers

Java class level lock vs. object level lock

If a thread T1 enters a method m1 by obtaining the class level lock, does this mean another thread T2 cannot run a different method m2 by obtaining the object level lock?
kgi
  • 421
  • 1
  • 5
  • 3
42
votes
3 answers

What's the least invasive way to read a locked file in C# (perhaps in unsafe mode)?

I need to read a Windows file that may be locked, but I don't want to create any kind lock that will prevent other processes from writing to the file. In addition, even if the file is locked for exclusive use, I'd like to see what's inside. Although…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
42
votes
6 answers

Releasing Windows file share locks

This problem crops up every now and then at work. Our build machine can have it's files accessed via a normal windows file share. If someone browses a folder remotely on the machine, and leaves the window open overnight, then the build fails (as it…
Jonathan Wright
  • 14,880
  • 4
  • 24
  • 19
42
votes
1 answer

Static versus non-static lock object in synchronized block

Trying to visualize and understand synchronization. What are the differences between using a static lock object (code A) and a non-static lock object (code B) for a synchronized block? How does it differ in practical applications? What are the…
ADTC
  • 8,999
  • 5
  • 68
  • 93
40
votes
5 answers

Biased locking in java

I keep reading about how biased locking, using the flag -XX:+UseBiasedLocking, can improve the performance of un-contended synchronization. I couldn't find a reference to what it does and how it improves the performance. Can anyone explain me what…
Chander Shivdasani
  • 9,878
  • 20
  • 76
  • 107
40
votes
3 answers

Try catch with locks in C++

In Java: Lock lock = new ReentrantLock(); try{ lock.lock(); someFunctionLikelyToCauseAnException(); } catch(e){...} finally { lock.unlock(); } My question is with this above example we know that the lock WILL always get unlocked because…
Brijendar Bakchodia
  • 1,567
  • 1
  • 9
  • 15
40
votes
4 answers

java.lang.IllegalMonitorStateException: (m=null) Failed to get monitor for

Why may this happen? The thing is that monitor object is not null for sure, but still we get this exception quite often: java.lang.IllegalMonitorStateException: (m=null) Failed to get monitor for (tIdx=60) at…
Andrey Adamovich
  • 20,285
  • 14
  • 94
  • 132
40
votes
10 answers

C++ mutex in namespace std does not name a type

I'm writing a simple C++ program to demonstrate the use of locks. I am using codeblocks and gnu gcc compiler. #include #include #include using namespace std; int x = 0; // shared variable void…
arjun
  • 2,333
  • 4
  • 21
  • 21
39
votes
9 answers

How to lock several objects?

I want to lock on two objects at the same time. Why can't I write like such code? lock (obj1, obj2) Should I always write like that? lock (obj1) { lock (obj2) { } } Probably this could be made simpler? Likely it would be better to…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
39
votes
5 answers

Asynchronous locking based on a key

I'm attempting to figure out an issue that has been raised with my ImageProcessor library here where I am getting intermittent file access errors when adding items to the cache. System.IO.IOException: The process cannot access the file…
James South
  • 10,147
  • 4
  • 59
  • 115
39
votes
6 answers

SemaphoreSlim.WaitAsync before/after try block

I know that in the sync world the first snippet is right, but what's about WaitAsync and async/await magic? Please give me some .net internals. await _semaphore.WaitAsync(); try { // todo } finally { _semaphore.Release(); } or try { …
sh1ng
  • 2,808
  • 4
  • 24
  • 38