473,394 Members | 1,278 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,394 software developers and data experts.

synclock and mutex

cj
I take it a mutex is like a synclock but visible outside the program?
Jun 1 '07 #1
2 3706
"cj" <cj@nospam.nospamwrote:
>I take it a mutex is like a synclock but visible outside the program?
Yea, that's a pretty good working definition.

There are some differences, but at the end of the day they're both
True/False flags for "Busy / Not Busy".

The Monitor (the thing that underlies the SyncLock) has some nice methods on
it like TryEnter and PulseAll that make it easy to use in some situtations.
It also has the handy VB keyword SyncLock that deals with the Try/Finally
semantics required to use a Monitor in an Exception safe way.

On the other hand, the Mutex is also a WaitHandle, and therefore works with
all of that infrastructure (WaitOne, WaitAll, WaitAny, etc), which makes it
easy to use in some situations. For example, you can register a WaitHandle
with the CLR threadpool, and get a callback when the WaitHandle is set.
There are some scenarios where this behavior is very handy.

Personally, I tend to use:
- Monitor (Synclock) for almost all of my locking.
- ManualResetEvents for almost all of my Waiting ("Is this done yet? Call me
back when it's done.")
- Mutex for ensuring an application is "single instance".

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
Jun 1 '07 #2
cj
Thanks. I've used synclock to make sure multiple threads don't trample
on each other when they are using a common class to say increment a
counter (to keep track of how many threads/transactions are currently
being handled) or write a log file. I was using:

<STAThread()Public Sub main(ByVal CmdArgs() As String)
Dim procArray() As System.Diagnostics.Process
procArray =
System.Diagnostics.Process.GetProcessesByName(Syst em.Diagnostics.Process.GetCurrentProcess.ProcessNa me)
If procArray.Length 1 Then
Dim thisProcHandle As Int32
thisProcHandle =
System.Diagnostics.Process.GetCurrentProcess.MainW indowHandle.ToInt32
For Each proc As System.Diagnostics.Process In procArray
Try
If Not proc.MainWindowHandle.ToInt32 = thisProcHandle Then
If IsIconic(proc.MainWindowHandle) Then
ShowWindow(proc.MainWindowHandle, 9)
End If
SetForegroundWindow(proc.MainWindowHandle)
End If
Catch
End Try
Next
Exit Sub
End If
Dim mainForm As New Form1
Application.Run(mainForm)
End Sub

in vb2003 for single instance apps. VB2005 has that covered for what
I'm doing in the project properties.

Mutex just came up when I wanted to run 2 instances of an app and have
one know it was A and the other B. I'm counting on nobody starting a
3rd or it too would become B which really wouldn't cause much of a
problem. I just want to know which app processed what data and separate
logs.

Anyway, thanks for the info.

Chris Mullins [MVP] wrote:
"cj" <cj@nospam.nospamwrote:
>I take it a mutex is like a synclock but visible outside the program?

Yea, that's a pretty good working definition.

There are some differences, but at the end of the day they're both
True/False flags for "Busy / Not Busy".

The Monitor (the thing that underlies the SyncLock) has some nice methods on
it like TryEnter and PulseAll that make it easy to use in some situtations.
It also has the handy VB keyword SyncLock that deals with the Try/Finally
semantics required to use a Monitor in an Exception safe way.

On the other hand, the Mutex is also a WaitHandle, and therefore works with
all of that infrastructure (WaitOne, WaitAll, WaitAny, etc), which makes it
easy to use in some situations. For example, you can register a WaitHandle
with the CLR threadpool, and get a callback when the WaitHandle is set.
There are some scenarios where this behavior is very handy.

Personally, I tend to use:
- Monitor (Synclock) for almost all of my locking.
- ManualResetEvents for almost all of my Waiting ("Is this done yet? Call me
back when it's done.")
- Mutex for ensuring an application is "single instance".
Jun 1 '07 #3

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

Similar topics

1
by: JennaS | last post by:
Hi. I'm new to multithreading applications in .NET. What exactly is the difference between using synclock on a variable or using mutex.waitone/mutex.releasemutex There is too much stuff out there on...
4
by: Ram | last post by:
Hey, I have about 5 Shared variables, that I want to be locked for access when they are written to. I'v read in the MSDN about the SyncLock statement, and I'm prety sure that's what I'm looking...
12
by: Keith Langer | last post by:
I have some questions about whether synclock is necessary in a few different scenarios: 1) I have a Queue class which is shared between two threads. Thread 1 pushes objects onto the queue and...
4
by: fred | last post by:
If I have multiple threads running a Sub as below then a number of threads can be held up at the SyncLock. When it becomes free which thread goes first. Is it just by chance which thread goes first...
4
by: Jeff Stewart | last post by:
Specifically, I don't understand the parameter that Synclock accepts. How is a reference type a lockable entity? What -is- a reference type? Is it a number? Is it a value at a specific memory...
1
by: fred | last post by:
I have a VB application that is using MS Access as its database. To avoid connection delays the application creates one connection to the database at start-up and maintains that single connection...
7
by: Chris Dunaway | last post by:
Suppose I have several threads that need to access the same object. ThreadA successfully acquires the lock using SyncLock. ThreadB attempts to acquire the lock and blocks and then ThreadC attempts...
2
by: Spam Catcher | last post by:
Hi all, I'm hosting a remoting service in IIS. I have a function which I only want one concurrent access at a time. Will this prevent multiple users from accessing the function at a paritcular...
2
by: HONOREDANCESTOR | last post by:
I have a buffer that needs to be locked sometimes, because 2 processes update it. So I made the buffer into a class and whenever there is code that affects it, I sandwich the code between ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.