473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

broken mutexing attempt

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();
}
}
}

Nov 16 '05 #1
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();
}
}
}

Nov 16 '05 #2
>> 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
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
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(,))
28
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...
18
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...
205
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
7
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....
28
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...
3
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 ## ## ## ...
1
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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...
0
Oralloy
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,...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.