473,746 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

threading & mutex: question about example

Hi all,

I've been trying to get my head around threading. Here's an example
from the book I'm reading:

/*************** ************/
Mutex m = null;
const string name = "xyz";

try
{
m = Mutex.OLpenExis ting(name);
}
catch (WaitHandleCann otBeOpenedExcep tion)
{ }

if (m == null)
{
// POINT 'A'
m = new Mutex(true, name);
}
else
{
m.close();
return;
}
/*************** **************/

This example can be used in the main method to prevent two instances
of the same application to be run.

Now, as a newbie to threading, it seems to me that theoretically two
threads might *both* get to "Point 'A'", correct?

Thanks, regards,
Jeroen
Jan 7 '08 #1
2 2057
Jeroen,

Yes, two threads might get to point 'A', but that's not really the
problem here. Because the Mutex is named, it is visible to other processes,
so you have to worry about another process opening the thread if it finds
out that it doesn't exist.

To be honest, this isn't the best way to open or create a Mutex which is
named, as you have this race condition.

It's a better idea to call the CreateMutex API function through the
P/Invoke layer, which will open or create the named mutex if it doesn't
exist. Then, with the handle returned from that function, you would set the
SafeWaitHandle property of a new Mutex instance to that handle and call the
appropriate methods.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Jeroen" <me******@gmail .comwrote in message
news:91******** *************** ***********@q39 g2000hsf.google groups.com...
Hi all,

I've been trying to get my head around threading. Here's an example
from the book I'm reading:

/*************** ************/
Mutex m = null;
const string name = "xyz";

try
{
m = Mutex.OLpenExis ting(name);
}
catch (WaitHandleCann otBeOpenedExcep tion)
{ }

if (m == null)
{
// POINT 'A'
m = new Mutex(true, name);
}
else
{
m.close();
return;
}
/*************** **************/

This example can be used in the main method to prevent two instances
of the same application to be run.

Now, as a newbie to threading, it seems to me that theoretically two
threads might *both* get to "Point 'A'", correct?

Thanks, regards,
Jeroen

Jan 7 '08 #2
Nicholas,

Thanks for the quick response. I understand your answer (for the
larger part), though I'm a bit confused on the term "P/Invoke layer".
However, that seemed a different topic altogether so I posted a new
message in the newsgroup here:

http://groups.google.nl/group/micros...3883d5cdd010e9

Thanks again for the mutex explanation.

Regards,
Jeroen
Jan 7 '08 #3

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

Similar topics

4
4092
by: Bardo | last post by:
Hi, I have a situation where I am capturing both a WMI event utilising the "ManagementEventWatcher" in the "System.Management" namespace, and a corresponding event ("EntryWritten") raised from the "EventLog" object in the "System.Diagnostics" namespace. When a certain event occurrs, both a WMI event is raised, and an event log entry is written. The problem I have is that I need to capture both events and somehow correlate which WMI...
3
1143
by: Steve B. | last post by:
Request comments on the code that should be wriiten: 1. Need users to start a application from local network but only allow one instance of the application to run on EACH users machine while, at the same time, allowing multiple network users to execute application (http://www.bobpowell.net/singleinstance.htm )? 2. Within this application are database forms (MS-Access, DataGrid, etc). For these forms, there should be only one network...
16
351
by: One Handed Man \( OHM - Terry Burns \) | last post by:
Sorry if this gets duplicated, but I posted it and cant see it for a long time so repost. . . I have an application which is writing to a graphics object, the main UI thread and a worker thread are writing to the same object, whats the best way to manage this ? Thanks OHM --
17
1451
by: One Handed Man \( OHM - Terry Burns \) | last post by:
Assumes a Form with a Panel on it., Does the Mutex have to be within the address of a thread start address ? Cheers - OHM '----------- *************** ---------------- Private endProgram As Boolean = False Dim image1 As Image = Image.FromFile("..\Images\gun.bmp")
2
1536
by: WXS | last post by:
When I see things in .NET 2.0 like obsoletion of suspend/resume because of the public reason MS gives of they think people are using them inappropriately.. use mutex, monitor and other synchronization objects instead and oh by the way you shouldn't be using them as they can cause deadlock and other major problems, screams bug or threading architecture issue and lack of willingness or priority to correct. In reality those API's are for...
6
1319
by: MobileBoy36 | last post by:
Hi All, I want to make a LogFile class that is thread safe. I use a Mutex for it. But the behavior of the class is not that normal. In a c# guide I read you can achieve it by simply using Mutex.Waitone and Mutex.ReleaseMutex. Is that right? What 's wrong with my sub Writelog then? Best regards, Mobileboy
9
1862
by: cgwalters | last post by:
Hi, I've recently been working on an application which does quite a bit of searching through large data structures and string matching, and I was thinking that it would help to put some of this CPU-intensive work in another thread, but of course this won't work because of Python's GIL. There's a lot of past discussion on this, and I want to bring it up again because with the work on Python 3000, I think it is worth trying
126
6722
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have obviously worked most of the kinks out of the generalized threading and processes idea (along with many...
7
6140
by: dmitrey | last post by:
hi all, Is there a better way to kill threading.Thread (running) instance than this one http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496960 (it's all I have found via google). BTW, it should be noticed that lots of threading module methods have no docstrings (in my Python 2.5), for example _Thread__bootstrap, _Thread__stop.
0
8975
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8801
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9516
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9351
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9286
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8229
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6774
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4840
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.