Race conditions in django

Django 1.4+ supports select_for_update, in earlier versions you may execute raw SQL queries e.g. select … for update which depending on underlying DB will lock the row from any updates, you can do whatever you want with that row until the end of transaction. e.g. from django.db import transaction @transaction.commit_manually() def add_points(request): user = User.objects.select_for_update().get(id=request.user.id) … Read more

Java thread dump: Difference between “waiting to lock” and “parking to wait for”?

You will get “waiting to lock” in the thread dump when using intrinsic locks and “parking to wait for” when using locks from java.util.concurrent. Consider the following example: import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class LockTest { final Lock lock = new ReentrantLock(true); synchronized void intrinsicLock() { Thread th = new Thread(new Runnable() { public void … Read more

Is it ok to read a shared boolean flag without locking it when another thread may set it (at most once)?

It is never OK to read something possibly modified in a different thread without synchronization. What level of synchronization is needed depends on what you are actually reading. For primitive types, you should have a look at atomic reads, e.g. in the form of std::atomic<bool>. The reason synchronization is always needed is that the processors … Read more

If a synchronized method calls another non-synchronized method, is there a lock on the non-synchronized method

If a synchronized method calls another non-synchronized method, is there a lock on the non-synchronized method The answer depends on the context. If you are in a synchronized method for an object, then calls by other threads to other methods of the same object instance that are also synchronized are locked. However calls by other … Read more

Why is it a bad practice to lock the object we are going to change?

From the C# language reference here: 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 this guideline: lock (this) is a problem if the instance can be accessed publicly. lock (typeof (MyType)) is a problem if MyType is … Read more

ReaderWriterLockSlim and async\await

ReaderWriterLockSlim is a thread-affine lock type, so it usually cannot be used with async and await. You should either use SemaphoreSlim with WaitAsync, or (if you really need a reader/writer lock), use my AsyncReaderWriterLock from AsyncEx or Stephen Toub’s AsyncReaderWriterLock.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)