Avatar of JRumana
JRumana

asked on 

Mutex Lock

I have a code sinppet with lock given below i want to remove the lock and introduce mutex how i can do that? What is the use of mutex than lock please educate me....

lock (object)
{
                        StreamReader streamReader = new StreamReader(stream);

                        int nReturnValue = ReadStream(streamReader);
                        //stream.Close();
                        streamReader.Close();
                        return nReturnValue;}
}
Programming Languages-OtherC#

Avatar of undefined
Last Comment
abel
Avatar of abel
abel
Flag of Netherlands image

A Mutex is something you use for interprocess communication (or between threads) and is basically nothing more than a named object. You can set access control to a mutex the way you would to a file.

You cannot set a mutex "lock" on a file. There is no such thing. What are you trying to achieve? Maybe another technology is more up to the task?

Cheers,
-- Abel --
Avatar of JRumana
JRumana

ASKER

Using the above i will read a file and in one more (write()) method i will write soem text in the same file thats why we used lock in the above code and now we are trying to achieve the same thing with mutex...
Avatar of abel
abel
Flag of Netherlands image

Maybe I misunderstand you, but a Mutex just has nothing to do with files. They are used for locking and unlocking a piece of code. Of course, inside this piece of code you can use a file. And using mutexes you could make sure that only one thread or process at the time is writing to the file.

If it is about threads, I suggest you use the keyword lock, which is there for exactly that reason.

If you need to exchange additional information between processes, you can use named mutexes. But since you haven't yet made clear what you want it is hard to give you a solid advice on what would be the best approach.

Working with mutexes is done with WaitOne (which will block execution until another process has released the mutex) and ReleaseMutex, which you must call when you are finished. You cannot release a mutex that you do not own. Owning is acquired by WaitOne. If you already own the mutex, then WaitOne will not stop execution.

Hope this gives you some more insight in the why's and when's of mutexes.

Cheers,
-- Abel --
Avatar of abel
abel
Flag of Netherlands image

Just thought you wanted also a hands on example. Though the things going on inside are rather complex, the actual calling / releasing of mutexes is really easy. See this C# code:

Mutex mut = new Mutex(false, "my_globally_unique_mutex");
mut.WaitOne();
// do your file thing
mut.ReleaseMutex();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Here's how you can use mutex. Assuming you have multiple threads, this enables you to ensure that only 1 thread is executing the "protected code" at a time. And just like abel said, lock will achieve the same thing, and is easier to use.

Btw, by using Mutex / lock, you will only synchronise threads within the same app. Supposing there is an external app that reads the file at the same time, you cannot control that through Mutex.

Create mutex in main thread
Mutex mutex = new Mutex();


Then do this in worker thread
mutex.WaitOne();
// your code here
mutex.ReleaseMutex();
Avatar of abel
abel
Flag of Netherlands image

> by using Mutex / lock, you will only synchronise threads within the same app.

lock >> yes
mutex >> no, that's global

> external app that reads the file at the same time, you cannot control that through Mutex.

make sure you read that correctly: a Mutex is global on the system and makes sure the executed code is exclusive. But any process not listening to your mutex rules (and that's about every app in the world) will not obey when it comes to the file. In other words:

  1. your code runs exclusively
  2. any sources you use know nothing of the mutex and can be accessed by a variety of means
  3. you still need to use file locks to protect the file from being exclusively accessed
Regards,
-- Abel --
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo