locking

Bad Fairness with a ReadWriteLock / SharedLock under load

Hello, we are currently designing a multithreaded server application. To optimize performance, we decided to implement a ReadWriteLock, i.e. multiple threads can get a lock if they only want to read but just one thread may hold the write lock. This lock is used with a list and iterating over the list is the "read" operation. Now this...

How can I lock the screen using C#?

I just write my first C# application, which is a scheduler. Once an hour I want to pop-up a dialog and lock the screen for two minutes in order to take a break. As of now, my application just shows a form on "TopMost" when its time to break and hides it two minutes later. How can I lock the screen as well? Something similar to the UA...

Locking hierarchy of objects in .NET

I have a class that represents the "state of the world". The class has many collections of many other objects which in turn have references to more objects and collections of objects, sometimes even references to their ancestors in the "world hierarchy". To simplify what's being said, here is an example (transformed into XML, details omi...

Optimal lock file method

Windows has an option to open a file with exclusive access rights. Unix doesn't. In order to ensure exclusive access to some file or device, it is common practice in unix to use a lock file usually stored in the /var/lock directory. The C instruction open( "/var/lock/myLock.lock", O_RDWR | O_CREAT | O_EXCL, 0666 ) returns -1 if the l...

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 this guideline: * lock (this) is a problem if the instance can be accessed publicly. * ...

Synchronising access to an object that multiple threads can access

My question is partially inspired by this article written by Eric Lippert: http://blogs.msdn.com/ericlippert/archive/2009/10/19/what-is-this-thing-you-call-thread-safe.aspx Using Eric's example of a Queue class being accessed by multiple threads. If I have two distinct pieces of code that access the queue: class MyThreadTest { pr...

Locking and Updating a File Accordingly

Note: I have read other posts on how to lock and unlock a file. I didn't find anything special that I wasn't aware of. So I am gonna put my scenario here so that someone can give some suggestions out. In my experience, FileChannel.lock doesn't guarantee the situation of locking and unlocking of a File when different Objects from multipl...

.Net4, Monitor.Enter(lockObject, acquiredLock)

In .Net4, Monitor.Enter(Object) is marked as obsolete : [ObsoleteAttribute("This method does not allow its caller to reliably release the lock. Please use an overload with a lockTaken argument instead.")] public static void Enter( Object obj ) And there is a new method Monitor.Enter(lockObject, acquiredLock) with this usage : bo...

C# lock and code analysis warning CA2002

In my application I have a form that starts synchronization process and for number of reasons I want to allow only one synchronization to run at a time. So I've added a static bool field to my form indicating whether sync is in progress and added a lock to set this field to true if it wasn't already set so that first thread could start s...

Do sequence points prevent code reordering across critical section boundaries?

Suppose that one has some lock based code like the following where mutexes are used to guard against inappropriate concurrent read and write mutex.get() ; // get a lock. T localVar = pSharedMem->v ; // read something pSharedMem->w = blah ; // write something. pSharedMem->z++ ; // read and write something. mutex.release() ; // rel...

Multi Threading locks and monitor class not working

I have a file that is read and written to. I need to make sure when its been written to, nobody else will try to write to it. I put a lock on the whole function which allows to either read or write but I still get errors such as The process cannot access the file 'FILENAME' because it is being used by another process. public static TY...

Is this (Lock-Free) Queue Implementation Thread-Safe?

I am trying to create a lock-free queue implementation in Java, mainly for personal learning. The queue should be a general one, allowing any number of readers and/or writers concurrently. Would you please review it, and suggest any improvements/issues you find? Thank you. import java.util.concurrent.atomic.AtomicReference; public cl...

OptimisticLocking and @OneToMany(mappedBy=...) handling?

I have an AbstractEntity class as superclass for all my entites that defines an optimistic lock column like this: @Version private long lockVersion; Now I often get OptimisticLockingExceptions on entities, that are only changed in one the mappedBy relations similar to the following: @OneToMany(mappedBy = Property.PROPERTY_DESCRIPTOR,...

mysql hanging while importing database dump

We have a db dump import script from our production db that we use to rebuild our sandbox dbs. The syntax we use for this is mysql -u uname -ppass dbname < prod_db_export.sql. The script proceeds to create the first table and then do this: LOCK TABLES `ad` WRITE; /*!40000 ALTER TABLE `ad` DISABLE KEYS */; /*!40000 ALTER TABLE `ad` ENABL...

Slowdown identified: ReaderWriterLock(-1). May I use different locks?

Hi, after hours of tracking mysterious one or two seconds long lasting "freeze" I finally found out that its ReaderWriterLock(-1). It is server app and the lock here is held for writing to client collection. Im not familiar with locking so I would like to ask if there is not any better/faster way? How about using lock object to lock add/...

Lock used in Cache Item callback and other method doesn't seem to lock

Simplest explanation I can produce: In my .NET1.1 web app I create a file on disc, in the Render method, and add an item to the Cache to expire within, say, a minute. I also have a callback method, to be called when the cache item expires, which deletes the file created by Render. In the Page_Init method I try to access the file which t...

Form has my table locked down tight even after docmd.close

Sorry for the wall of text guys but this one requires expliaining, way too much code to post... I'm importing fixed width files into access in methods that require data entry. I import the file using transferText into two specs (ones global, the other is special circumstance). I have a function that uses DAO to cycle through all Field...

lock vs AcquireReader & writer locks

I have found possible slowdown in my app so I would have two questions: What is the real difference between simple locking on object and reader/writer locks? E.g. I have a collection of clients, that change quickly. For iterations should I use readerlock or the simple lock is enough? In order to decrease load, I have left iteration (on...

whats the difference between PESSIMISTIC_READ and PESSIMISTIC_WRITE?

I have read the article Locking and Concurrency in Java Persistence 2.0, and run the sample application. But i still cant realize the difference between PESSIMISTIC_READ and PESSIMISTIC_WRITE. I tried to modify the code, and where the code using PESSIMISTIC_READ and PESSIMISTIC_WRITE will have the same result that the sql will invoked wi...

ASP.NET - Different threads writing to the same file causing problems

I've got a method which performs a delete and create file. there are issues with the threads all trying to access the file at the same time. How can i limit access to the file? public static Save(string file) { //1.Perform Delete //2.Perform Write } Note that the method is static so is it possible to lock the process within the...