I've been using monitors a bit lately (some of you may have heard ;-) ) and
then up pop Manual and AutoResetEvents , and they look for all the world
like the same thing.
Are they interchangeable, or when should I use one over the other?
TIA
Charles 4 3156
Charles, I've been using monitors a bit lately (some of you may have heard ;-) )
and then up pop Manual and AutoResetEvents , and they look for all the world like the same thing.
Monitors are used to "synchronize access to a member or to build your own
thread management types".
Manual & Auto Reset Events are used to "signaled when an event occurs".
Are they interchangeable, or when should I use one over the other?
Yes they can be used interchangeable, and yes there are times when you
should use one over the other.
Normally I use SyncLock instead of using the Monitor directly, remember that
SyncLock is implemented in terms of Monitor. SyncLock is used to protect one
or more blocks of code from being executed simultaneously. For example if I
had a System.Collections.Queue to send requests from the Main thread to a
Worker thread. I would protect the methods where Queue.Enqueue &
Queue.Dequeue were with the same padlock object (the object passed to
Monitor/SyncLock).
I would use a Manual or Auto ResetEvent in the same class to let the worker
thread know there is work to be done.
Something like:
' untested, typed from memory.
Public Class ThreadRequestQueue
Private Readonly m_padlock As New Object
Private Readonly m_queue As New Queue
Private Readonly m_event As New AutoResetEvent(False)
' called from the Main thread
Public Sub AddRequest(ByVal request As Object)
SyncLock m_padlock
m_queue.Enqueue(Object)
End SyncLock
m_event.Set()
End Sub
' called from the Worker thread
Public Function GetRequest() As Object
' Check to see if there are already items available
SyncLock m_padlock
If m_queue.Count() > 0 Then
Return m_queue.DeQueue()
End If
End SyncLock
' Cannot block worker thread
' while waiting for the main thread to add requests
m_event.WaitOne()
' There must be an item
SyncLock m_padlock
Return m_queue.Dequeue()
End SyncLock
End Function
End Class
Hope this helps
Jay
"Charles Law" <bl***@nowhere.com> wrote in message
news:ep****************@TK2MSFTNGP09.phx.gbl... I've been using monitors a bit lately (some of you may have heard ;-) )
and then up pop Manual and AutoResetEvents , and they look for all the world like the same thing.
Are they interchangeable, or when should I use one over the other?
TIA
Charles
Hi Jay SyncLock is used to protect one or more blocks of code from being executed simultaneously.
Is this different from "accessing the same variable concurrently"?
What I mean is, if I have
<code>
Public Function Read() as Long
SyncLock (m_LockObject)
Return m_Variable
End SyncLock
End Function
Public Sub Write(Value As Long)
SyncLock (m_LockObject)
m_Variable = Value
End SyncLock
End Sub
</code>
can one thread execute Read while another executes Write, so that m_Variable
can be read when it is only half updated? After all, I am not executing the
same bit of code simultaneously because they are in two different methods.
I understand that two threads cannot execute Read or Write simultaneously,
but what about Read *and* Write?
Secondly,
m_event.Set()
Isn't this just Monitor.Pulse(m_event), and
m_event.WaitOne()
Monitor.Wait(m_event) ? If so, what is there to choose between the two?
Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Oh**************@TK2MSFTNGP12.phx.gbl... Charles, I've been using monitors a bit lately (some of you may have heard ;-) ) and then up pop Manual and AutoResetEvents , and they look for all the world like the same thing. Monitors are used to "synchronize access to a member or to build your own thread management types".
Manual & Auto Reset Events are used to "signaled when an event occurs".
Are they interchangeable, or when should I use one over the other? Yes they can be used interchangeable, and yes there are times when you should use one over the other.
Normally I use SyncLock instead of using the Monitor directly, remember
that SyncLock is implemented in terms of Monitor. SyncLock is used to protect
one or more blocks of code from being executed simultaneously. For example if
I had a System.Collections.Queue to send requests from the Main thread to a Worker thread. I would protect the methods where Queue.Enqueue & Queue.Dequeue were with the same padlock object (the object passed to Monitor/SyncLock).
I would use a Manual or Auto ResetEvent in the same class to let the
worker thread know there is work to be done.
Something like:
' untested, typed from memory. Public Class ThreadRequestQueue
Private Readonly m_padlock As New Object Private Readonly m_queue As New Queue Private Readonly m_event As New AutoResetEvent(False)
' called from the Main thread Public Sub AddRequest(ByVal request As Object) SyncLock m_padlock m_queue.Enqueue(Object) End SyncLock m_event.Set() End Sub
' called from the Worker thread Public Function GetRequest() As Object ' Check to see if there are already items available SyncLock m_padlock If m_queue.Count() > 0 Then Return m_queue.DeQueue() End If End SyncLock
' Cannot block worker thread ' while waiting for the main thread to add requests m_event.WaitOne()
' There must be an item SyncLock m_padlock Return m_queue.Dequeue() End SyncLock End Function
End Class
Hope this helps Jay
"Charles Law" <bl***@nowhere.com> wrote in message news:ep****************@TK2MSFTNGP09.phx.gbl... I've been using monitors a bit lately (some of you may have heard ;-) ) and then up pop Manual and AutoResetEvents , and they look for all the world like the same thing.
Are they interchangeable, or when should I use one over the other?
TIA
Charles
Charles, Is this different from "accessing the same variable concurrently"? ... I understand that two threads cannot execute Read or Write simultaneously, but what about Read *and* Write?
Simultaneously and concurrently are synonymous in that only one of either
Read or Write will execute at the same time. In other words if I have 2
threads. If Thread 1 is in either Read or Write Thread 2 cannot be in
either Read or Write. Like wise if Thread 2 is in either Read or Write,
Thread 1 cannot be in either Read or Write. I'm really not sure what you are
wanting to say with "*and*".
Secondly, m_event.Set() Isn't this just Monitor.Pulse(m_event), and m_event.WaitOne() Monitor.Wait(m_event) ?
Go back to the original statement. Monitor can be used "or to build your own
thread management types". So yes you can use Monitor to build the two Event
classes. I would not be surprised if the two Event classes are implemented
in terms of Monitor.
If so, what is there to choose between the two?
One word: Abstraction
As I said before I would use Manual or AutoResetEvent if I wanted to signal
an Event between two threads. (notice Event in the name of the class).
I would use Monitor (SyncLock really) to control access to a resource (code
block).
Hope this helps
Jay
"Charles Law" <bl***@nowhere.com> wrote in message
news:uj*************@TK2MSFTNGP10.phx.gbl... Hi Jay
SyncLock is used to protect one or more blocks of code from being executed simultaneously. Is this different from "accessing the same variable concurrently"?
What I mean is, if I have
<code> Public Function Read() as Long SyncLock (m_LockObject) Return m_Variable End SyncLock End Function
Public Sub Write(Value As Long) SyncLock (m_LockObject) m_Variable = Value End SyncLock End Sub </code>
can one thread execute Read while another executes Write, so that
m_Variable can be read when it is only half updated? After all, I am not executing
the same bit of code simultaneously because they are in two different methods.
I understand that two threads cannot execute Read or Write simultaneously, but what about Read *and* Write?
Secondly,
m_event.Set() Isn't this just Monitor.Pulse(m_event), and
m_event.WaitOne()
Monitor.Wait(m_event) ? If so, what is there to choose between the two?
Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:Oh**************@TK2MSFTNGP12.phx.gbl... Charles, I've been using monitors a bit lately (some of you may have heard
;-) ) and then up pop Manual and AutoResetEvents , and they look for all the
world like the same thing. Monitors are used to "synchronize access to a member or to build your
own thread management types".
Manual & Auto Reset Events are used to "signaled when an event occurs".
Are they interchangeable, or when should I use one over the other? Yes they can be used interchangeable, and yes there are times when you should use one over the other.
Normally I use SyncLock instead of using the Monitor directly, remember that SyncLock is implemented in terms of Monitor. SyncLock is used to protect one or more blocks of code from being executed simultaneously. For example
if I had a System.Collections.Queue to send requests from the Main thread to
a Worker thread. I would protect the methods where Queue.Enqueue & Queue.Dequeue were with the same padlock object (the object passed to Monitor/SyncLock).
I would use a Manual or Auto ResetEvent in the same class to let the worker thread know there is work to be done.
Something like:
' untested, typed from memory. Public Class ThreadRequestQueue
Private Readonly m_padlock As New Object Private Readonly m_queue As New Queue Private Readonly m_event As New AutoResetEvent(False)
' called from the Main thread Public Sub AddRequest(ByVal request As Object) SyncLock m_padlock m_queue.Enqueue(Object) End SyncLock m_event.Set() End Sub
' called from the Worker thread Public Function GetRequest() As Object ' Check to see if there are already items available SyncLock m_padlock If m_queue.Count() > 0 Then Return m_queue.DeQueue() End If End SyncLock
' Cannot block worker thread ' while waiting for the main thread to add requests m_event.WaitOne()
' There must be an item SyncLock m_padlock Return m_queue.Dequeue() End SyncLock End Function
End Class
Hope this helps Jay
"Charles Law" <bl***@nowhere.com> wrote in message news:ep****************@TK2MSFTNGP09.phx.gbl... I've been using monitors a bit lately (some of you may have heard
;-) ) and then up pop Manual and AutoResetEvents , and they look for all the
world like the same thing.
Are they interchangeable, or when should I use one over the other?
TIA
Charles
Jay
Thanks for the further clarification.
Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Of*************@tk2msftngp13.phx.gbl... Charles, Is this different from "accessing the same variable concurrently"? ... I understand that two threads cannot execute Read or Write
simultaneously, but what about Read *and* Write? Simultaneously and concurrently are synonymous in that only one of either Read or Write will execute at the same time. In other words if I have 2 threads. If Thread 1 is in either Read or Write Thread 2 cannot be in either Read or Write. Like wise if Thread 2 is in either Read or Write, Thread 1 cannot be in either Read or Write. I'm really not sure what you
are wanting to say with "*and*".
Secondly, m_event.Set() Isn't this just Monitor.Pulse(m_event), and m_event.WaitOne() Monitor.Wait(m_event) ?
Go back to the original statement. Monitor can be used "or to build your
own thread management types". So yes you can use Monitor to build the two
Event classes. I would not be surprised if the two Event classes are implemented in terms of Monitor.
If so, what is there to choose between the two? One word: Abstraction
As I said before I would use Manual or AutoResetEvent if I wanted to
signal an Event between two threads. (notice Event in the name of the class).
I would use Monitor (SyncLock really) to control access to a resource
(code block).
Hope this helps Jay
"Charles Law" <bl***@nowhere.com> wrote in message news:uj*************@TK2MSFTNGP10.phx.gbl... Hi Jay
SyncLock is used to protect one or more blocks of code from being executed simultaneously. Is this different from "accessing the same variable concurrently"?
What I mean is, if I have
<code> Public Function Read() as Long SyncLock (m_LockObject) Return m_Variable End SyncLock End Function
Public Sub Write(Value As Long) SyncLock (m_LockObject) m_Variable = Value End SyncLock End Sub </code>
can one thread execute Read while another executes Write, so that m_Variable can be read when it is only half updated? After all, I am not executing the same bit of code simultaneously because they are in two different
methods. I understand that two threads cannot execute Read or Write
simultaneously, but what about Read *and* Write?
Secondly,
m_event.Set()
Isn't this just Monitor.Pulse(m_event), and
m_event.WaitOne()
Monitor.Wait(m_event) ? If so, what is there to choose between the two?
Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message news:Oh**************@TK2MSFTNGP12.phx.gbl... Charles, > I've been using monitors a bit lately (some of you may have heard ;-) ) and > then up pop Manual and AutoResetEvents , and they look for all the world > like the same thing. Monitors are used to "synchronize access to a member or to build your own thread management types".
Manual & Auto Reset Events are used to "signaled when an event
occurs". > Are they interchangeable, or when should I use one over the other? Yes they can be used interchangeable, and yes there are times when you should use one over the other.
Normally I use SyncLock instead of using the Monitor directly,
remember that SyncLock is implemented in terms of Monitor. SyncLock is used to
protect one or more blocks of code from being executed simultaneously. For example if I had a System.Collections.Queue to send requests from the Main thread
to a Worker thread. I would protect the methods where Queue.Enqueue & Queue.Dequeue were with the same padlock object (the object passed to Monitor/SyncLock).
I would use a Manual or Auto ResetEvent in the same class to let the
worker thread know there is work to be done.
Something like:
' untested, typed from memory. Public Class ThreadRequestQueue
Private Readonly m_padlock As New Object Private Readonly m_queue As New Queue Private Readonly m_event As New AutoResetEvent(False)
' called from the Main thread Public Sub AddRequest(ByVal request As Object) SyncLock m_padlock m_queue.Enqueue(Object) End SyncLock m_event.Set() End Sub
' called from the Worker thread Public Function GetRequest() As Object ' Check to see if there are already items available SyncLock m_padlock If m_queue.Count() > 0 Then Return m_queue.DeQueue() End If End SyncLock
' Cannot block worker thread ' while waiting for the main thread to add requests m_event.WaitOne()
' There must be an item SyncLock m_padlock Return m_queue.Dequeue() End SyncLock End Function
End Class
Hope this helps Jay
"Charles Law" <bl***@nowhere.com> wrote in message news:ep****************@TK2MSFTNGP09.phx.gbl... > I've been using monitors a bit lately (some of you may have heard ;-) ) and > then up pop Manual and AutoResetEvents , and they look for all the world > like the same thing. > > Are they interchangeable, or when should I use one over the other? > > TIA > > Charles > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Franco, Gustavo |
last post by:
Hi, I have a question, and please I need a answer.
How can I finalize a thread running with Application.Run (I need the message
loop!!!) without call Thread.Abort?.
I want to call...
|
by: rmunson8 |
last post by:
I have a derived class from the Queue base class. I need it to be
thread-safe, so I am using the Synchronized method (among other things out of
scope of this issue). The code below compiles, but...
|
by: Phil |
last post by:
I have an application that has a main thread and a worker thread.
From time to time, the main thread, because of user input, needs to notify
the worker thread to execute an extra bit of code, in...
|
by: Maqsood Ahmed |
last post by:
Hello,
We have been experiencing a problem with AutoResentEvent class for past
2-3 months. It seems that it just stops at WaitOne and the thread
doesn't released from blocking state. We have...
|
by: Manjunath Premkumar |
last post by:
I have an application which uses AutoResetEvent for synchronization. I have deployed this application in a web server. This is working fine. When this application is deployed in environments where...
|
by: buu |
last post by:
so, I have a private object as system.threading.AutoResetEvent, and I would
like to read it's current status.
currently I have an another boolean object wich I update together with an...
|
by: timor.super |
last post by:
Hi all,
Imagine I've an array of int :
int anArray = new int;
I want to extract all the integer that are superior to 500
I can do :
|
by: mycita |
last post by:
Hello,
The Database server = DB2/NT Workgroup Edition 8.2.8
Recently we configured the db to do online backups. After the initial full offline backup full online backups have been scheduled...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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: 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,...
|
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: 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...
|
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...
| |