No, you can't. It doesn't. Just return isMouseInside.
No that can't deadlock; there is only one lock object, and no extension point exists that would allow you to do something messy while the lock is held. However, if you are using lock you should probably make it clear what scenarios you are trying to avoid. While the meaning is actually very subtle, I wonder whether volatile might work here without needing any locks.
Or Interlocked on an int that is always either 0 or 1. But sure, it looks like it'll work; bool is always atomic anyway, so the lock here is only really acting as a memory-barrier avoiding cache issues (hence why volatile might also work). And remember that any time you get the value, that is now stale and might already be incorrect.It was true at the point of read, though.
Ive stoped using volatile since I read this: albahari. Com/threading/part4.aspx. – Pedro77 Sep 12 at 12:53 @Pedro yes, volatile is complex and subtle - but in this case it would serve the same purpose – Marc Gravell?
Sep 12 at 14:50 I just want to know at moment of the read, no problem if it changes just after becouse I will check it again when I need. I will take your advise and I will read more about volatiles. – Pedro77 Sep 13 at 11:50.
You can't deadlock - you are locking on the same object. It doesn't make sense to me. Also the locking doesn't make sense - I don't think you achieve anything with it.
What is the goal?
UI thread updates IsMouseInside. Another thread will read its value some times, that is why Im using the lock. – Pedro77 Sep 11 at 22:44 All right, but you are locking for a single operation.
You don't need to do that. – Petar Ivanov Sep 12 at 3:02 Sorry, can you explain? Locking a bool is only usefull for memory barrier since it is an atomic variable?
– Pedro77 Sep 12 at 12:56.
You can only have a deadlock if you have two different locks A and B that two different threads try to acquire in different order - since you only have one lock here you are safe. Also keep in mind that C# lock (which is syntactic sugar for using a Monitor) is reentrant - a single thread cannot deadlock since it can reenter a lock as many times as it wants to once it has initially acquired it.
I think I can deadlock with something like X += X, and X is a int and uses the kind of lock that I did. – Pedro77 Sep 11 at 22:45 @Pedro: That still would work since lock is reentrant: en.wikipedia. Org/wiki/Reentrant_mutex – BrokenGlass Sep 11 at 22:49 @Pedro how would X+=X be a deadlock?
At worst it could be data loss due to conflicts (Interlocked. Add would be preferred), but that is different to a deadlock. – Marc Gravell?
Sep 11 at 22:52 @BrokenGlass in that scenario re-entrancy is unrelated; that is 2 "get"s and a "set" - but each separately. Unless of course you explicitly lock for the duration, but then the locks in the accessors seem unnecessary. – Marc Gravell?
Sep 11 at 23:00.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.