Locking files in linux with c/c++

fcntl() is the one API to choose, since it is the least broken and is POSIX. It is the only one that works across NFS. That said it is a complete disaster, too, since locks are bound to processes, not file descriptors. That means that if you lock a file and then some other thread … Read more

android unlock screen intent?

Yes, the ACTION_USER_PRESENT is broadcasted after the user unlocks: http://developer.android.com/reference/android/content/Intent.html#ACTION_USER_PRESENT Note that this is a protected broadcast and if the user is using a lock screen replacement such as WidgetLocker or NoLock the USER_PRESENT may not be sent or may be sent at the wrong time. For detecting WidgetLocker‘s unlock see: http://teslacoilsw.com/widgetlocker/developers

How to avoid race condition when using a lock-file to avoid two instances of a script running simultaneously?

Yes, there is indeed a race condition in the sample script. You can use bash’s noclobber option in order to get a failure in case of a race, when a different script sneaks in between the -f test and the touch. The following is a sample code-snippet (inspired by this article) that illustrates the mechanism: … Read more

Guidelines of when to use locking

The best guide for locking and threading I found, is this page (this is the text I consult when working with locking and threading): http://www.albahari.com/threading/ You want the paragraph “Locking and Thread Safety”, but read the rest as well, it is very well written.

How to lock on an integer in C#?

I like doing it like this public class Synchronizer { private Dictionary<int, object> locks; private object myLock; public Synchronizer() { locks = new Dictionary<int, object>(); myLock = new object(); } public object this[int index] { get { lock (myLock) { object result; if (locks.TryGetValue(index, out result)) return result; result = new object(); locks[index] = result; … Read more

Windows 2008 R2 – Kernel (System Process PID=4) is locking files and folders

As Dani has already mentioned in the comment: It’s a bug in Windows 7 and likely in Windows Server 2008 (possibly 64bit versions only). It surfaces when you disable Application Experience service. Re-enabling this service has fixed this problem for me. A bit more info here as to why it’s causing a problem. List of … Read more

increment a count value outside parallel.foreach scope

I like to beat dead horses! 🙂 The “lightest” way to increment the count from multiple threads is: Interlocked.Increment(ref count); But as others have pointed out: if you’re doing it inside Parallel.ForEach then you’re probably doing something wrong. I’m suspecting that for some reason you’re using ForEach but you need an index to the item … Read more

How do I use NSConditionLock? Or NSCondition

EDIT: as @Bonshington commented, this answer refers to NSCondition (as opposed to NSConditionLock): – (void) method1 { [myCondition lock]; while (!someCheckIsTrue) [myCondition wait]; // Do something. [myCondition unlock]; } – (void) method2 { [myCondition lock]; // Do something. someCheckIsTrue = YES; [myCondition signal]; [myCondition unlock]; } The someCheckIsTrue can be anything, it could be a … Read more