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
933
votes
13 answers

Optimistic vs. Pessimistic locking

I understand the differences between optimistic and pessimistic locking. Now, could someone explain to me when I would use either one in general? And does the answer to this question change depending on whether or not I'm using a stored procedure…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
777
votes
10 answers

Volatile vs. Interlocked vs. lock

Let's say that a class has a public int counter field that is accessed by multiple threads. This int is only incremented or decremented. To increment this field, which approach should be used, and why? lock(this.locker)…
core
  • 32,451
  • 45
  • 138
  • 193
640
votes
10 answers

What is the difference between lock, mutex and semaphore?

I've heard these words related to concurrent programming, but what's the difference between lock, mutex and semaphore?
victor
  • 6,411
  • 3
  • 16
  • 5
635
votes
9 answers

What does a lock statement do under the hood?

I see that for using objects which are not thread safe we wrap the code with a lock like this: private static readonly Object obj = new Object(); lock (obj) { // thread unsafe code } So, what happens when multiple threads access the same code…
NLV
  • 21,141
  • 40
  • 118
  • 183
534
votes
18 answers

Why is lock(this) {...} bad?

The MSDN documentation says that public class SomeObject { public void SomeOperation() { lock(this) { //Access instance variables } } } is "a problem if the instance can be accessed publicly". I'm wondering why? Is it…
Anton
  • 6,860
  • 12
  • 30
  • 26
487
votes
9 answers

Why can't I use the 'await' operator within the body of a lock statement?

The await keyword in C# (.NET Async CTP) is not allowed from within a lock statement. From MSDN: An await expression cannot be used in a synchronous function, in a query expression, in the catch or finally block of an exception handling statement,…
Kevin
  • 8,312
  • 4
  • 27
  • 31
421
votes
23 answers

Is there an advantage to use a Synchronized Method instead of a Synchronized Block?

Can any one tell me the advantage of synchronized method over synchronized block with an example?
Warrior
  • 39,156
  • 44
  • 139
  • 214
310
votes
4 answers

std::lock_guard or std::scoped_lock?

C++17 introduced a new lock class called std::scoped_lock. Judging from the documentation it looks similar to the already existing std::lock_guard class. What's the difference and when should I use it?
Stephan Dollberg
  • 32,985
  • 16
  • 81
  • 107
227
votes
11 answers

Java synchronized method lock on object, or method?

If I have 2 synchronized methods in the same class, but each accessing different variables, can 2 threads access those 2 methods at the same time? Does the lock occur on the object, or does it get as specific as the variables inside the synchronized…
wuntee
  • 12,170
  • 26
  • 77
  • 106
220
votes
7 answers

Do spurious wakeups in Java actually happen?

Seeing various locking related question and (almost) always finding the 'loop because of spurious wakeups' terms1 I wonder, has anyone experienced such kind of a wakeup (assuming a decent hardware/software environment for example)? I know the term…
akarnokd
  • 69,132
  • 14
  • 157
  • 192
214
votes
5 answers

Effect of NOLOCK hint in SELECT statements

I guess the real question is: If I don't care about dirty reads, will adding the with (NOLOCK) hint to a SELECT statement affect the performance of: the current SELECT statement other transactions against the given table Example: Select *…
Bob Probst
  • 9,533
  • 8
  • 32
  • 41
212
votes
8 answers

Concurrent HashSet in .NET Framework?

I have the following class. class Test{ public HashSet Data = new HashSet(); } I need to change the field "Data" from different threads, so I would like some opinions on my current thread-safe implementation. class Test{ …
kukab
  • 2,133
  • 2
  • 12
  • 4
209
votes
6 answers

How efficient is locking and unlocked mutex? What is the cost of a mutex?

In a low level language (C, C++ or whatever): I have the choice in between either having a bunch of mutexes (like what pthread gives me or whatever the native system library provides) or a single one for an object. How efficient is it to lock a…
Albert
  • 65,406
  • 61
  • 242
  • 386
205
votes
8 answers

Recursive Lock (Mutex) vs Non-Recursive Lock (Mutex)

POSIX allows mutexes to be recursive. That means the same thread can lock the same mutex twice and won't deadlock. Of course it also needs to unlock it twice, otherwise no other thread can obtain the mutex. Not all systems supporting pthreads also…
Mecki
  • 125,244
  • 33
  • 244
  • 253
199
votes
18 answers

What is a deadlock?

When writing multi-threaded applications, one of the most common problems experienced are deadlocks. My questions to the community are: What is a deadlock? How do you detect them? Do you handle them? And finally, how do you prevent them from…
bmurphy1976
  • 29,564
  • 11
  • 33
  • 24
1
2 3
99 100