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
49
votes
5 answers

synchronized block - lock more than one object

I'm modelling a game where multiple players (threads) move at the same time. The information of where a player is located at the moment is stored twice: the player has a variable "hostField" that references to a field on the board and every field…
speendo
  • 13,045
  • 22
  • 71
  • 107
49
votes
2 answers

mutex.lock vs unique_lock

When should I prefer the first piece of code to the second, and do they have fundamental differences std::mutex mtx; mtx.lock(); ... //protected stuff mtx.unlock(); ... //non-protected stuff mtx.lock(); ... //etc and std::mutex…
Morgan Fouque
  • 643
  • 1
  • 7
  • 17
49
votes
6 answers

C# How to detect an object is already locked

How can I detect whether an object is locked or not? Monitor.TryEnter (as described in Is there a way to detect if an object is locked?) does not work for me because it locks the object if it is not locked. I only want to check if it is locked and…
hwcverwe
  • 5,287
  • 7
  • 35
  • 63
49
votes
6 answers

Race conditions in django

Here is a simple example of a django view with a potential race condition: # myapp/views.py from django.contrib.auth.models import User from my_libs import calculate_points def add_points(request): user = request.user user.points +=…
Fragsworth
  • 33,919
  • 27
  • 84
  • 97
48
votes
2 answers

SQL atomic increment and locking strategies - is this safe?

I have a question about SQL and locking strategies. As an example, suppose I have a view counter for the images on my website. If I have a sproc or similar to perform the following statements: START TRANSACTION; UPDATE images SET counter=counter+1…
Alexander Torstling
  • 18,552
  • 7
  • 62
  • 74
48
votes
6 answers

Why is "lock (typeof (MyType))" a problem?

MSDN gives the following warning about the lock keyword in C#: In general, avoid locking on a public type, or instances beyond your code's control. The common constructs lock (this), lock (typeof (MyType)), and lock ("myLock") violate…
Alex K
  • 10,835
  • 8
  • 29
  • 34
48
votes
4 answers

What is lock-free multithreaded programming?

I have seen people/articles/SO posts who say they have designed their own "lock-free" container for multithreaded usage. Assuming they haven't used a performance-hitting modulus trick (i.e. each thread can only insert based upon some modulo) how can…
user997112
  • 29,025
  • 43
  • 182
  • 361
48
votes
4 answers

How would you implement your own reader/writer lock in C++11?

I have a set of data structures I need to protect with a readers/writer lock. I am aware of boost::shared_lock, but I would like to have a custom implementation using std::mutex, std::condition_variable and/or std::atomic so that I can better…
jack
  • 2,094
  • 1
  • 19
  • 17
45
votes
3 answers

MySQL InnoDB: Difference Between `FOR UPDATE` and `LOCK IN SHARE MODE`

What is the exact difference between the two locking read clauses: SELECT ... FOR UPDATE and SELECT ... LOCK IN SHARE MODE And why would you need to use one over the other?
pje
  • 21,801
  • 10
  • 54
  • 70
44
votes
3 answers

Read Locks and Write Locks

I am a little unsure about read and write locks and just need someone to check if these facts about read/write locks are correct. This is in reference to databases in general. Read Locks: Multiple read locks can be acquired by multiple threads at…
nknj
  • 2,436
  • 5
  • 31
  • 45
44
votes
1 answer

std::scoped_lock or std::unique_lock or std::lock_guard?

From this question I understand that std::scoped_lock is "a strictly superior version of std::lock_guard". From this question I understand that "std::lock_guard and std::unique_lock are the same" except that std::unique_lock has some extra features…
lachy
  • 1,833
  • 3
  • 18
  • 27
44
votes
4 answers

Is it OK to use a string as a lock object?

I need to make a critical section in an area on the basis of a finite set of strings. I want the lock to be shared for the same string instance, (somewhat similar to String.Intern approach). I am considering the following implementation: public…
Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
44
votes
5 answers

Do I need to lock or mark as volatile when accessing a simple boolean flag in C#?

Lets just say you have a simple operation that runs on a background thread. You want to provide a way to cancel this operation so you create a boolean flag that you set to true from the click event handler of a cancel button. private bool…
Simon P Stevens
  • 27,303
  • 5
  • 81
  • 107
43
votes
10 answers

lock keyword in C#

I understand the main function of the lock key word from MSDN lock Statement (C# Reference) The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and…
David Basarab
  • 72,212
  • 42
  • 129
  • 156
43
votes
11 answers

Why is ConcurrentBag so slow in .Net (4.0)? Am I doing it wrong?

Before I started a project, I wrote a simple test to compare the performance of ConcurrentBag from (System.Collections.Concurrent) relative to locking & lists. I am extremely surprised that ConcurrentBag is over 10 times slower than locking with a…
Tachy
  • 971
  • 1
  • 9
  • 14