locking

Can I design any lock-free solution for this scenario

柔情痞子 提交于 2019-12-10 12:26:42
问题 I have a simple Employee class as follows public class Employee { public int ID { get; set; } public string LastName { get; set; } public string FirstName { get; set; } } I then have a ProcessEmployees class which do concurrent processing of these employees in method called ProcessThisEmployee. Within this method, I have to call a third party library method. So far its all simple. Problem is sometime when user choose to cancel this operation when its in progress, I need to do some clean up on

Java ReentrantLock.unlock/await()/signal() not throwing IllegalMonitorStateException

☆樱花仙子☆ 提交于 2019-12-10 12:14:58
问题 Where am I going wrong? Even though my consumer Thread is not holding the lock, the program is not throwing IllegalMonitorStateException for any of the lock calls (unlock/await/signal). Update: private final ReentrantLock lock = new ReentrantLock(); private final Condition producers = lock.newCondition(); private final Condition consumers = lock.newCondition(); @Override public void run() { while (true) { try { //lock.lockInterruptibly(); try { while (sharedResource.isEmpty()) { printErr(name

Java fine-grained synchronization in getter/setter methods and singleton pattern of a class

醉酒当歌 提交于 2019-12-10 11:59:53
问题 I'm trying to use the synchronization java directive to implement fine-grained synchronization in a class, i.e. synchronize the least amount of code I can.. I'll comment the code inline, to explain what I do and after the code I'll ask you how to improve the code: public class MyClass { private static volatile MyClass singletonInstance = null; private HashMap<String, Integer> mHashMap = null; private String mStringA = null; private String mStringB = null; // Use double check technique to use

Write-Once, Read-Many Lock

折月煮酒 提交于 2019-12-10 11:58:52
问题 I know this topic has been pounded as I have read gillions of posts on SO as well as elsewhere about it, but I have yet to find a definitive answer, so my apologies if you are offended by this seeming redundancy. I have a situation where there is a write-once, read-millions resource. The resource is extremely expensive to create and the contention on the write-lock could be very high as well. Furthermore, I cannot predict what processor this will be run on and so I do not know what the memory

SQL Server 2005: Key-Range Locks in Read Committed Transaction Isolation Level?

不想你离开。 提交于 2019-12-10 11:45:53
问题 I'm helping troubleshoot some deadlocking in a .NET application that uses SQL Server 2005. I have the XML data from the trace below. What really puzzles me is the RangeX-X lock on PK_Exp_Experience_PriorFirm when the transaction isolation level is read committed. Everything I've read indicates that you only get a key-range lock of you are using the transaction isolation level "serializable". So far, I can't find any places in our application where we set the isolation level to anything other

What is problem on this lock?

淺唱寂寞╮ 提交于 2019-12-10 11:08:10
问题 Highly there is a problem in this lock but i couldn't understand what is that. I have strong suspicious that below example doesn't lock enough well. So what can be problem ? class example { object locker = new object(); void start() { for (int i = 0; i < 1000; i++) { (new Thread(dostuff)).Start(); } } void dostuff() { lock (locker) { //dosomething } } } 回答1: Your code creates 1000 Threads. That is enormously expensive, requiring over 1 GB of memory. And then all those threads compete for a

Does python's fcntl.flock function provide thread level locking of file access?

浪子不回头ぞ 提交于 2019-12-10 10:46:20
问题 Python's fcnt module provides a method called [flock][1] to proved file locking. It's description reads: Perform the lock operation op on file descriptor fd (file objects providing a fileno() method are accepted as well). See the Unix manual flock(2) for details. (On some systems, this function is emulated using fcntl().) Looking up the linux man page for flock, it only refers to cross process locking, for example: A call to flock() may block if an incompatible lock is held by another process

Linux timers on global vars

℡╲_俬逩灬. 提交于 2019-12-10 10:45:01
问题 I found the below code in the Internet, I'm trying to understand how Linux timer works, anyway, as you can see below the counter1 is global var, what will happen if the while is working on it and the timer goes off and changed the value of counter1, do I need a lock in there? // timertst1.c: Simple timer demo program. Use cross-gcc // Vers. 1.00 - 21.Oct. 2002 // k.d.walter@t-online.de #include <stdio.h> #include <string.h> #include <signal.h> #include <sys/time.h> // This is a timer handler.

Why can't I directly access (and lock) the implicit lock that Objects use for synchronized block

懵懂的女人 提交于 2019-12-10 10:34:09
问题 rephrased for clarity I would like to be able to mix the use of the synchronized block with more explicit locking via calling lock and release methods directly when appropriate. Thus allowing me the syntaxtical sugar of using sychtronized(myObject) when I can get away with it, but also being able to call myObject.lock & myObject.unlock dierctly for those times when a synchronized block is not flexible enough to do what I need done. I know that every single Object has, implicitly, a rentrant

Multiple object locks in Java?

偶尔善良 提交于 2019-12-10 10:29:41
问题 Is it safe/acceptable practice to lock on a private field variable (instead of using a lock object)? This way, I could have different locks for different purposes. Example below: class Test { private Integer x = 0; private Integer y = 0; public void incrementX() { synchronized(x) { x++; } } public void decrementX() { synchronized(x) { x++; } } public void incrementY() { synchronized(y) { y++; } } public void decrementY() { synchronized(y) { y++; } } Or should I have a lock object for each