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
119
votes
4 answers

Java Synchronized Block for .class

What does this java code mean? Will it gain lock on all objects of MyClass? synchronized(MyClass.class) { //is all objects of MyClass are thread-safe now ?? } And how the above code differs from this one: synchronized(this) { //is all objects…
user249739
116
votes
3 answers

What is the difference between Lock and RLock

From the docs: threading.RLock() -- A factory function that returns a new reentrant lock object. A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again…
BufBills
  • 8,005
  • 12
  • 48
  • 90
111
votes
9 answers

How to implement a lock in JavaScript

How could something equivalent to lock in C# be implemented in JavaScript? So, to explain what I'm thinking a simple use case is: User clicks button B. B raises an onclick event. If B is in event-state the event waits for B to be in ready-state…
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
110
votes
9 answers

Monitor vs lock

When is it appropriate to use either the Monitor class or the lock keyword for thread safety in C#? EDIT: It seems from the answers so far that lock is short hand for a series of calls to the Monitor class. What exactly is the lock call short-hand…
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
108
votes
2 answers

Confused about UPDLOCK, HOLDLOCK

While researching the use of Table Hints, I came across these two questions: Which lock hints should I use (T-SQL)? What effect does HOLDLOCK have on UPDLOCK? Answers to both questions say that when using (UPDLOCK, HOLDLOCK), other processes will…
Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
107
votes
5 answers

Can I limit concurrent invocations of an AWS Lambda?

I have a Lambda function that’s triggered by a PUT to an S3 bucket. I want to limit this Lambda function so that it’s only running one instance at a time – I don’t want two instances running concurrently. I’ve had a look through the Lambda…
alexwlchan
  • 5,699
  • 7
  • 38
  • 49
99
votes
6 answers

Does a locked object stay locked if an exception occurs inside it?

In a c# threading app, if I were to lock an object, let us say a queue, and if an exception occurs, will the object stay locked? Here is the pseudo-code: int ii; lock(MyQueue) { MyClass LclClass = (MyClass)MyQueue.Dequeue(); try { ii…
Khadaji
  • 2,147
  • 2
  • 17
  • 19
98
votes
5 answers

Parallel.ForEach with adding to list

I'm trying to run multiple functions that connect to a remote site (by network) and return a generic list. But I want to run them simultaneously. For example: public static List Search(string title) { //Initialize a new temp list…
shaharmor
  • 1,636
  • 3
  • 14
  • 26
96
votes
4 answers

Is a bool read/write atomic in C#

Is accessing a bool field atomic in C#? In particular, do I need to put a lock around: class Foo { private bool _bar; //... in some function on any thread (or many threads) _bar = true; //... same for a read if (_bar) { ... } }
dbkk
  • 12,643
  • 13
  • 53
  • 60
95
votes
8 answers

Why is volatile used in double checked locking

From Head First design patterns book, the singleton pattern with double checked locking has been implemented as below: public class Singleton { private volatile static Singleton instance; private Singleton() {} public static Singleton…
toc777
  • 2,607
  • 2
  • 26
  • 37
92
votes
8 answers

Locking Executing Files: Windows does, Linux doesn't. Why?

I noticed when a file is executed on Windows (.exe or .dll), it is locked and cannot be deleted, moved or modified. Linux, on the other hand, does not lock executing files and you can delete, move, or modify them. Why does Windows lock when Linux…
David Lenihan
  • 921
  • 1
  • 7
  • 3
91
votes
5 answers

Why the Global Interpreter Lock?

What is exactly the function of Python's Global Interpreter Lock? Do other languages that are compiled to bytecode employ a similar mechanism?
Federico A. Ramponi
  • 46,145
  • 29
  • 109
  • 133
89
votes
4 answers

Forcing a query timeout in SQL Server

We have had an issue with a block of code that responds poorly in the face of slow databases (It craps the bed on a query timeout). We have created a patch, and are in the process of running it through regression. We can't get a timeout. I've opened…
BnWasteland
  • 2,109
  • 1
  • 18
  • 14
88
votes
3 answers

lock(new object()) -- Cargo cult or some crazy "language special case"?

I'm reviewing some code written by a consultant, and while dozens of red flags have already popped up, I can't wrap my head around the following snippet: private void foo() { if (InvokeRequired) { lock (new object()) { …
user434817
87
votes
11 answers

When is ReaderWriterLockSlim better than a simple lock?

I'm doing a very silly benchmark on the ReaderWriterLock with this code, where reading happens 4x more often than writting: class Program { static void Main() { ISynchro[] test = { new Locked(), new RWLocked() }; Stopwatch…
vtortola
  • 34,709
  • 29
  • 161
  • 263