locking

Filter Lock Algorithm

前提是你 提交于 2019-12-21 15:29:09
问题 I'm busy looking at the filter lock algorithm for n-thread mutual exclusion and I can't seem to understand line 17 of the code. I understand that it is spinning on a condition but not entirely sure what those conditions are. More specifically what (∃k != me) entails. 1 class Filter implements Lock { 2 int[] level; 3 int[] victim; 4 public Filter(int n) { 5 level = new int[n]; 6 victim = new int[n]; // use 1..n-1 7 for (int i = 0; i < n; i++) { 8 level[i] = 0; 9 } 10 } 11 public void lock() {

Filter Lock Algorithm

萝らか妹 提交于 2019-12-21 15:27:08
问题 I'm busy looking at the filter lock algorithm for n-thread mutual exclusion and I can't seem to understand line 17 of the code. I understand that it is spinning on a condition but not entirely sure what those conditions are. More specifically what (∃k != me) entails. 1 class Filter implements Lock { 2 int[] level; 3 int[] victim; 4 public Filter(int n) { 5 level = new int[n]; 6 victim = new int[n]; // use 1..n-1 7 for (int i = 0; i < n; i++) { 8 level[i] = 0; 9 } 10 } 11 public void lock() {

Java: What, if anything, is locked by synchronized methods apart from the object they belong to?

人盡茶涼 提交于 2019-12-21 15:02:08
问题 Now, I'm not sure whether this is a stupid question, please bear with me if it is. Is the lock on an object "recursive", i. e. if two objects have references to a third object in their fields and a thread is running a synchronized method on one of the two, can any other thread access the third object? // a and b are some objects that implement Runnable // they both reference the same third object a.ref = c; b.ref = c; // a is run in a thread and processes some data in a loop for a long time /

Does locking ensure reads and writes are flushed from caches? If so, how?

扶醉桌前 提交于 2019-12-21 12:43:04
问题 I was reading this MSDN article on lockless thread syncing. The article seems to infer that as long as you enter a lock before accessing shared variables, then those variables will be up to date (in .Net 2.0 at least). I got to thinking how this was possible? A lock in .Net is just some arbitrary object that all threads check before accessing memory, but the lock itself has no knowledge of the memory locations that are being accessed. If I have a thread updating a variable, or even a whole

Does PESSIMISTIC_WRITE lock the whole table?

纵饮孤独 提交于 2019-12-21 12:12:26
问题 Just to be sure that I correctly understand how things work. If I do em.lock(employee, LockModeType.PESSIMISTIC_WRITE); - will it block only this entity ( employee ) or the whole table Employees ? If it matters, I am talking about PostgreSQL . 回答1: It should block only the entity. PostgreSQL hibernate dialect adds for update in case of write locks: https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java#L549 (newer

How can I write a conditional lock in C#?

让人想犯罪 __ 提交于 2019-12-21 11:37:02
问题 The thing is I've been using the lock statement to protect a critical part of my code, but now, I realize I could allow concurrent execution of that critical code is some conditions are met. Is there a way to condition the lock? 回答1: Action doThatThing = someMethod; if (condition) { lock(thatThing) { doThatThing(); } } else { doThatThing(); } 回答2: I think that question cries "race condition!". What if the condition turns from true to false shortly after the check, but before a thread enters

Are Java wait(), notify() 's implementation significantly different from locks?

不打扰是莪最后的温柔 提交于 2019-12-21 10:18:41
问题 Out of curiosity, when Java implements wait() and notify() methods, are they really just using locks? i.e., wait() acquires a mutex, notify() release a mutex, notifyAll() releases all mutexes (in the same object, of course)? Other than being less cumbersome than using locks, are there other advantages of using wait() and notify()? [EDIT] I realized what I confused myself about after Brian's comments: wait doesn't lock, it releases the lock and passes it to someone else who's waiting on a

Why lock when reading from a dictionary

独自空忆成欢 提交于 2019-12-21 09:54:51
问题 I am confused by a code listing in a book i am reading, C# 3 in a Nutshell, on threading. In the topic on Thread Safety in Application Servers, below code is given as an example of a UserCache: static class UserCache { static Dictionary< int,User> _users = new Dictionary< int, User>(); internal static User GetUser(int id) { User u = null; lock (_users) // Why lock this??? if (_users.TryGetValue(id, out u)) return u; u = RetrieveUser(id); //Method to retrieve from databse lock (_users) _users

How to use Multiple Variables for a lock Scope in C#

旧巷老猫 提交于 2019-12-21 09:53:11
问题 I have a situation where a block of code should be executed only if two locker objects are free. I was hoping there would be something like: lock(a,b) { // this scope is in critical region } However, there seems to be nothing like that. So does it mean the only way for doing this is: lock(a) { lock(b) { // this scope is in critical region } } Will this even work as expected? Although the code compiles, but I am not sure whether it would achieve what I am expecting it to. 回答1: I'd expect it to

MySQL from the command line - can I practically use LOCKs?

一笑奈何 提交于 2019-12-21 09:23:26
问题 I'm doing a bash script that interacts with a MySQL datatabase using the mysql command line programme. I want to use table locks in my SQL. Can I do this? mysql -e "LOCK TABLES mytable" # do some bash stuff mysql -u "UNLOCK TABLES" The reason I ask, is because table locks are only kept for the session, so wouldn't the lock be released as soon as that mysql programme finishes? 回答1: [EDIT] nos had the basic idea -- only run "mysql" once, and the solution nos provided should work, but it left