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

C# lock statement, what object to lock on?

I have 3 questions that I need help with. What are the correct objects/references to be passed as lock statement parameter? I've seen a lot of sample codes and I noticed that the objects/references passed in could possibly be non related to the…
user1885498
  • 823
  • 1
  • 8
  • 19
62
votes
5 answers

boost::unique_lock vs boost::lock_guard

I don't well understand the difference betweeen these two lock classes. In boost documentation it is said, boost::unique_lock doesn't realize lock automatically. Does it mean that the main difference between unique_lock and lock_guard is that with…
Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
62
votes
9 answers

C# thread safety with get/set

This is a detail question for C#. Suppose I've got a class with an object, and that object is protected by a lock: Object mLock = new Object(); MyObject property; public MyObject MyProperty { get { return property; } set { …
mmr
  • 14,781
  • 29
  • 95
  • 145
62
votes
4 answers

Why doesn't Lock'ing on same object cause a deadlock?

Possible Duplicate: Re-entrant locks in C# If I write some code like this: class Program { static void Main(string[] args) { Foo(); Console.ReadLine(); } static void Foo() { lock(_lock) { …
Eric
  • 5,842
  • 7
  • 42
  • 71
61
votes
5 answers

Difference between manual locking and Synchronized methods

Is there any difference between this: internal class MyClass { private readonly object _syncRoot = new Object(); public void DoSomething() { lock(_syncRoot) { ... } } public void…
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
61
votes
9 answers

What is the difference between a lock and a latch in the context of concurrent access to a database?

I am trying to understand a paper on concurrent B-tree, in which the author mentioned latch vs lock, and how latches do not need a "Lock Manager". I have been trying to figure out what are differences between those two for two days. Google resulting…
Viele
  • 2,244
  • 2
  • 19
  • 31
61
votes
10 answers

Pure-Ruby concurrent Hash

What's the best way to implement a Hash that can be modified across multiple threads, but with the smallest number of locks. For the purposes of this question, you can assume that the Hash will be read-heavy. It must be thread-safe in all Ruby…
Yehuda Katz
  • 28,535
  • 12
  • 89
  • 91
60
votes
6 answers

Distributed Lock Service

Which distributed lock service would you use? Requirements are: A mutual exclusion (lock) that can be seen from different processes/machines lock...release semantics Automatic lock release after a certain timeout - if lock holder dies, it will…
ripper234
  • 222,824
  • 274
  • 634
  • 905
59
votes
9 answers

How to find out what is locking my tables?

I have a SQL table that all of a sudden cannot return data unless I include with (nolock) on the end, which indicates some kind of lock left on my table. I've experimented a bit with sys.dm_tran_locks to identify that there are in fact a number of…
59
votes
5 answers

Reference for proper handling of PID file on Unix

Where can I find a well-respected reference that details the proper handling of PID files on Unix? On Unix operating systems, it is common practice to “lock” a program (often a daemon) by use of a special lock file: the PID file. This is a file in a…
bignose
  • 30,281
  • 14
  • 77
  • 110
59
votes
7 answers

How long will a C# lock wait, and what if the code crashes during the lock?

I saw the following code, and wanted to use it for a simple activity which may only be executed one at a time, and won't occur frequently (so the chance of occurring twice at a time is very small, but you never know). So the code: // class…
Michel
  • 23,085
  • 46
  • 152
  • 242
58
votes
1 answer

T-SQL: Lock a table manually for some minutes

I know this will be strange, but I want to trigger an error in my MVC application and this error will be based on a LINQ Query, where I would like to get one record from a table. While this record will be blocked (database/table/row) level using…
Richárd Baldauf
  • 1,068
  • 2
  • 10
  • 24
58
votes
5 answers

ReentrantReadWriteLock: what's the difference between ReadLock and WriteLock?

What I know is: ReadLock and WriteLock affect each other somehow WriteLock is just like synchronized ReadLock seems cannot work alone
DunkOnly
  • 1,682
  • 4
  • 17
  • 39
57
votes
11 answers

How to easily make std::cout thread-safe?

I have a multi-threaded application, which heavily uses std::cout for logging without any locking. In such a case, how can I easily add lock mechanism to make std::cout thread-safe? I don't want to search for each occurrence of std::cout and add a…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
56
votes
5 answers

What is the difference between synchronized on lockObject and using this as the lock?

I know the difference between synchronized method and synchronized block but I am not sure about the synchronized block part. Assuming I have this code class Test { private int x=0; private Object lockObject = new Object(); public void…
GantengX
  • 1,471
  • 2
  • 16
  • 28