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.

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

readLock.lock(); This means that if any other thread is writing (i.e. holds a write lock) then stop here until no other thread is writing. Once the lock is granted no other thread will be allowed to write (i.e. take a write lock) until the lock is released. writeLock.lock(); This means that if any other thread … Read more

Are locks unnecessary in multi-threaded Python code because of the GIL?

You will still need locks if you share state between threads. The GIL only protects the interpreter internally. You can still have inconsistent updates in your own code. For example: #!/usr/bin/env python import threading shared_balance = 0 class Deposit(threading.Thread): def run(self): for _ in xrange(1000000): global shared_balance balance = shared_balance balance += 100 shared_balance = … Read more

Optimal lock file method

Take a look at the enlightening presentation File Locking Tricks and Traps: This short talk presents several common pitfalls of file locking and a few useful tricks for using file locking more effectively. Edit: To address your questions more precisely: Is there a better synchronization method than the lock file? As @Hasturkun already mentioned and … 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

EF Core takes a lot of time, sometimes, to perform SELECT query

It is not new question when SQL Server may slowdown queries because of Parameter Sniffing. Problem can be solved by converting parameters to constants or by adding OPTION(RECOMPILE) to the end of the query. This answer adds DbCommandInterceptor to DbContextOptions and appends OPTION(RECOMPILE) hint to particular queries. Configuring DbContext builder.UseSqlServer(connectionString) .UseRecompileExtensions(); // registering interceptor How … Read more

Threads – Why a Lock has to be followed by try and finally

Because a try/finally block is the only way to guarantee that a segment of code is executed after another completes. You ask why not do this: public void function1(){ read.lock(); this.getSharedInt(); read.unlock(); } What happens when this.getSharedInt() throws an exception? Then your read.unlock() line will not be executed, causing program deadlock. Sure, it may be … Read more

what is a loader lock?

For example, review this question: Loader lock error The general idea of loader lock: The system runs the code in DllMain inside a lock (as in – synchronization lock). Therefore, running non-trivial code inside DllMain is “asking for a deadlock” Answer I’ve mentioned is based on this article: Another reason not to do anything scary … Read more