locking

Lock, mutex, semaphore… what's the difference?

亡梦爱人 提交于 2019-12-17 04:36:14
问题 I've heard these words related to concurrent programming, but what's the difference between them? 回答1: A lock allows only one thread to enter the part that's locked and the lock is not shared with any other processes. A mutex is the same as a lock but it can be system wide (shared by multiple processes). A semaphore does the same as a mutex but allows x number of threads to enter, this can be used for example to limit the number of cpu, io or ram intensive tasks running at the same time. For

Does a locked object stay locked if an exception occurs inside it?

时光毁灭记忆、已成空白 提交于 2019-12-17 04:22:44
问题 In a c# threading app, if I were to lock an object, let us say a queue, and if an exception occurs, will the object stay locked? Here is the pseudo-code: int ii; lock(MyQueue) { MyClass LclClass = (MyClass)MyQueue.Dequeue(); try { ii = int.parse(LclClass.SomeString); } catch { MessageBox.Show("Error parsing string"); } } As I understand it, code after the catch doesn't execute - but I have been wondering if the lock will be freed. 回答1: First; have you considered TryParse? in li; if(int

What is the difference between synchronized on lockObject and using this as the lock?

霸气de小男生 提交于 2019-12-17 04:18:31
问题 I know the difference between synchronized method and synchronized block but I am not sure about the synchronized block part. Assuming I have this code class Test { private int x=0; private Object lockObject = new Object(); public void incBlock() { synchronized(lockObject) { x++; } System.out.println("x="+x); } public void incThis() { // same as synchronized method synchronized(this) { x++; } System.out.println("x="+x); } } In this case what is the difference between using lockObject and

What is the difference between synchronized on lockObject and using this as the lock?

試著忘記壹切 提交于 2019-12-17 04:18:06
问题 I know the difference between synchronized method and synchronized block but I am not sure about the synchronized block part. Assuming I have this code class Test { private int x=0; private Object lockObject = new Object(); public void incBlock() { synchronized(lockObject) { x++; } System.out.println("x="+x); } public void incThis() { // same as synchronized method synchronized(this) { x++; } System.out.println("x="+x); } } In this case what is the difference between using lockObject and

What is the difference between synchronized on lockObject and using this as the lock?

孤者浪人 提交于 2019-12-17 04:18:04
问题 I know the difference between synchronized method and synchronized block but I am not sure about the synchronized block part. Assuming I have this code class Test { private int x=0; private Object lockObject = new Object(); public void incBlock() { synchronized(lockObject) { x++; } System.out.println("x="+x); } public void incThis() { // same as synchronized method synchronized(this) { x++; } System.out.println("x="+x); } } In this case what is the difference between using lockObject and

Is Task.Factory.StartNew() guaranteed to use another thread than the calling thread?

半腔热情 提交于 2019-12-17 03:24:26
问题 I am starting a new task from a function but I would not want it to run on the same thread. I don't care which thread it runs on as long as it is a different one (so the information given in this question does not help). Am I guaranteed that the below code will always exit TestLock before allowing Task t to enter it again? If not, what is the recommended design pattern to prevent re-entrency? object TestLock = new object(); public void Test(bool stop = false) { Task t; lock (this.TestLock) {

Java Synchronized Block for .class

无人久伴 提交于 2019-12-17 03:22:34
问题 What does this java code mean? Will it gain lock on all objects of MyClass ? synchronized(MyClass.class) { //is all objects of MyClass are thread-safe now ?? } And how the above code differs from this one: synchronized(this) { //is all objects of MyClass are thread-safe now ?? } 回答1: The snippet synchronized(X.class) uses the class instance as a monitor. As there is only one class instance (the object representing the class metadata at runtime) one thread can be in this block. With

Confused about UPDLOCK, HOLDLOCK

安稳与你 提交于 2019-12-17 02:54:12
问题 While researching the use of Table Hints, I came across these two questions: Which lock hints should I use (T-SQL)? What effect does HOLDLOCK have on UPDLOCK? Answers to both questions say that when using (UPDLOCK, HOLDLOCK) , other processes will not be able to read data on that table, but I didn't see this. To test, I created a table and started up two SSMS windows. From the first window, I ran a transaction that selected from the table using various table hints. While the transaction was

SQL Server - How to lock a table until a stored procedure finishes

不打扰是莪最后的温柔 提交于 2019-12-17 02:26:30
问题 I want to do this: create procedure A as lock table a -- do some stuff unrelated to a to prepare to update a -- update a unlock table a return table b Is something like that possible? Ultimately I want my SQL server reporting services report to call procedure A, and then only show table a after the procedure has finished. (I'm not able to change procedure A to return table a). 回答1: Needed this answer myself and from the link provided by David Moye, decided on this and thought it might be of

Reader/Writer Locks in C++

断了今生、忘了曾经 提交于 2019-12-17 02:09:11
问题 I'm looking for a good reader/writer lock in C++. We have a use case of a single infrequent writer and many frequent readers and would like to optimize for this. Preferable I would like a cross-platform solution, however a Windows only one would be acceptable. 回答1: Newer versions of boost::thread have read/write locks (1.35.0 and later, apparently the previous versions did not work correctly). They have the names shared_lock, unique_lock, and upgrade_lock and operate on a shared_mutex. 回答2: