How to lock several objects?
Well, this question is way too old but, here is a compact one I figured out, both codes will end up to the same compiled statements (this and the one in the question description): lock (obj1) lock (obj2) { // your code }
Well, this question is way too old but, here is a compact one I figured out, both codes will end up to the same compiled statements (this and the one in the question description): lock (obj1) lock (obj2) { // your code }
Essentially, if your objects are locked only by one thread, the JVM can make an optimization and “bias” that object to that thread in such a way that subsequent atomic operations on the object incurs no synchronization cost. I suppose this is typically geared towards overly conservative code that performs locks on objects without ever … Read more
I happened to be looking at the same problem. GCC works fine with std::mutex under Linux. However, on Windows things seem to be worse. In the <mutex> header file shipped with MinGW GCC 4.7.2 (I believe you are using a MinGW GCC version too), I have found that the mutex class is defined under the … Read more
See the javadoc for Object.wait. in particular “The current thread must own this object’s monitor.” and “[throws] IllegalMonitorStateException – if the current thread is not the owner of the object’s monitor.” That is, you need to synchronize on the object you are going to call wait on. so your code should be: synchronized (available) { … Read more
For this we use the RAII-style construct std::lock_guard. When you use std::mutex m; { // start of some scope std::lock_guard lg(m); // stuff } // end of scope lg will ensure that m will be unlocked no matter what path the scope is left as it is destroyed at scope exit and std::lock_guards destructor will … Read more
The difference is simple: if the locked-on object is in a static field, then all instances of MyClass* will share that lock (i.e. no two objects will be able to lock on that object at the same time). If the field is non-static, then each instance will have its own lock, so only calls of … Read more
If you are admin on the server sharing the file over the network, you can use the Windows in-built feature: Start → My Computer → Right-click → Manage gets you to the Computer Management console In the left nav, navigate to Systems Tools → Shared Folders You can view Shares, Sessions & Open Files here. … Read more
You can do it without copying the file, see this article: The trick is to use FileShare.ReadWrite (from the article): private void LoadFile() { try { using(FileStream fileStream = new FileStream( “logs/myapp.log”, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using(StreamReader streamReader = new StreamReader(fileStream)) { this.textBoxLogs.Text = streamReader.ReadToEnd(); } } } catch(Exception ex) { MessageBox.Show(“Error loading log file: … Read more
You cannot lock on a null value because the CLR has no place to attach the SyncBlock to, which is what allows the CLR to synchronize access to arbitrary objects via Monitor.Enter/Exit (which is what lock uses internally)
No, it doesn’t mean that. The “class level lock” is just a regular lock on a different object, namely SomeClass.class. The “object level lock” locks on this. Edit: Just to make sure I’m following your understanding of the terminology, you’re wondering if m1 and m2 can be run concurrently as they are defined below: public … Read more