What is wrong with this code? What I expect from a mutex is that it can
only be locked once. There may be multiple objects pointing to the same
mutex (as there are in the sample code below), but I expect that once a
mutex is locked, no other object, no other process, nothing, should be able
to acquire a lock on that object until the first lock is released.
Instead, I see that both mutexes m1 and m2 are able to claim locks
simultaneously. That's broken, in my opinion, and not at all what I want.
How can I cause two handles to the same mutex to remain mutually
exclusive -- even from within the same thread?
using System;
using System.Threading;
namespace ConsoleApplication356
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
bool createdNew;
//creates a new mutex
Mutex m1 = new Mutex(false, "MyMutex123", out createdNew);
Console.WriteLine("created new = {0}", createdNew);
//creates a second mutex object with a handle to the first object
Mutex m2 = new Mutex(false, "MyMutex123", out createdNew);
Console.WriteLine("created new = {0}", createdNew);
//lock the mutex named "MyMutex123"
Console.WriteLine("Claimed m1 = {0}", m1.WaitOne(10, false));
//this should fail, since "MyMutex123" is already locked, but it
merely
//acquires a second lock! how can i cause this second lock attempt to
fail?
Console.WriteLine("Claimed m2 = {0}", m2.WaitOne(10, false));
//clean up
m2.ReleaseMutex();
m1.ReleaseMutex();
}
}
} 2 1006
The same *thread* can request the same mutex several times. The thread
must release the mutex the same number of times it acquires it.
Regards,
Joakim
RFPOP3 wrote: What is wrong with this code? What I expect from a mutex is that it can only be locked once. There may be multiple objects pointing to the same mutex (as there are in the sample code below), but I expect that once a mutex is locked, no other object, no other process, nothing, should be able to acquire a lock on that object until the first lock is released.
Instead, I see that both mutexes m1 and m2 are able to claim locks simultaneously. That's broken, in my opinion, and not at all what I want. How can I cause two handles to the same mutex to remain mutually exclusive -- even from within the same thread?
using System; using System.Threading;
namespace ConsoleApplication356 { class Class1 { [STAThread] static void Main(string[] args) { bool createdNew;
//creates a new mutex Mutex m1 = new Mutex(false, "MyMutex123", out createdNew); Console.WriteLine("created new = {0}", createdNew);
//creates a second mutex object with a handle to the first object Mutex m2 = new Mutex(false, "MyMutex123", out createdNew); Console.WriteLine("created new = {0}", createdNew);
//lock the mutex named "MyMutex123" Console.WriteLine("Claimed m1 = {0}", m1.WaitOne(10, false));
//this should fail, since "MyMutex123" is already locked, but it merely //acquires a second lock! how can i cause this second lock attempt to fail? Console.WriteLine("Claimed m2 = {0}", m2.WaitOne(10, false));
//clean up m2.ReleaseMutex(); m1.ReleaseMutex(); } } }
>> Instead, I see that both mutexes m1 and m2 are able to claim locks simultaneously. That's broken, in my opinion, and not at all what I want. How can I cause two handles to the same mutex to remain mutually exclusive -- even from within the same thread?
Think about it. What you are suggesting is that a thread should be able
to dead lock with itself. How would that get resolved? In your code, if
m2.WaitOne() would hang unti m1 was released, nothing would happen
because m1 cannot be released because the m1.ReleaseMutex() statement
will never be reached...
Regards,
Joakim This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Humpdydum |
last post by:
Can anyone try the following in their python interpreter?
These give correct output:
>>> print list(ndiff(,))
>>> print list(ndiff(,))
>>> print list(ndiff(,))
|
by: Grant Edwards |
last post by:
I finally figured out why one of my apps sometimes fails under
Win32 when it always works fine under Linux: Under Win32, the
pickle module only works with a subset of floating point
values. In...
|
by: John at Free Design |
last post by:
Thanks in advance for the help and let me know if this should be posted to
another MSDN.
I am developing a web-based application in VS.NET2003 using VB. When I
insert a graphic into a project...
|
by: Jeremy Siek |
last post by:
CALL FOR PAPERS/PARTICIPATION
C++, Boost, and the Future of C++ Libraries
Workshop at OOPSLA
October 24-28, 2004
Vancouver, British Columbia, Canada
http://tinyurl.com/4n5pf
Submissions
|
by: duckfreezone |
last post by:
Hi,
I've got a small test program which behaves correctly (IMHO) on all
compilers that I have acccess to, with the exception of the gcc on
Macintosh OSX "gcc version 4.0.0 (Apple Computer, Inc....
|
by: Craig Cockburn |
last post by:
I have a tool which tells me the number of times that visitors attempt
to access a link from my site to an external site and what the response
code received was. In the event of the remote site...
|
by: mensanator |
last post by:
## Holy Mother of Pearl!
##
## >>> for i in range(10):
## for j in range(10):
## print '%4d' % (gmpy.mpz(i)*gmpy.mpz(j)),
## print
##
##
## ...
|
by: Chris Mullins |
last post by:
We've been using the SSLStream class found in System.Net.Security to build a
giant Sockets server that provides TLS encryption at the channel leve.
Before .Net 2.0, we used an open-source...
|
by: RobG |
last post by:
I tried posting a comment on the AjaxToolbox support forum, however
that seems to be broken. Next best is to post here, hoping someone
appropriate may read it (you there Matt?). ;-)
In the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |