473,397 Members | 2,028 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,397 software developers and data experts.

Multiple threads accessing Shared functions

VS 2003, vb.net , sql msde...

I have an application with multiple threads running. Its a telephony
application where each thread represents a telephone line. For code that
would be the same for each thread, I put in Shared methods as below. It is
only now that I am realizing the complexity of multiple threads accessing
shared methods. And, quite honestly, I am very confused.

I have tried System.Threading.Monitor.Enter, Synclock, etc., and still
cannot get things to work right. Honestly, it seems to be that for each
shared method, you need to do something to lock it in the 1st line and
unlock it when you exit. Otherwise, anything at anytime can be changed at
any moment by any thread in that method.

For example, if you have a variable loaded with a value at the top of the
method, that you then set a column to that variable while adding a row, you
cannot just use System.Threading.Monitor.Enter(DataRowToAdd) to protect the
row your are adding, because one of the variables might change by another
thread. You would need to use System.Threading.Monitor.Enter on everything
(variables, etc.) adding quite a bit of code. Even at that, I don't see
that System.Threading.Monitor.Enter stops another thread anyway.

I have spend a lot of time one this, and don't understand how to do it. Can
you explain to me how to write a shared method that adds rows to a data set,
and updates that dataset to the datasource, that can be used by multiple
threads at the same time. Any URLs would be helpf ul to. Maybe if I see a
working example, I will grasp what is going on.

Thanks!
Bob Day

Public NotInheritable Class DL
Public Shared Sub tblMETHODS_DETAILSC_ADD_Row(ByVal
tblMethods_Summary_Row_Number_0_Based As Integer, ByVal Summary_Text As
String, Optional ByVal Details_Text As String = "")

' code to add row to table methods_details and update that row to
datasource of same name

end sub

end class
Nov 20 '05 #1
9 5372
Use a MuteX variable and then have your threads enter a wait stait for when
the variable is unlocked...
"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:ed**************@TK2MSFTNGP09.phx.gbl...
VS 2003, vb.net , sql msde...

I have an application with multiple threads running. Its a telephony
application where each thread represents a telephone line. For code that
would be the same for each thread, I put in Shared methods as below. It is only now that I am realizing the complexity of multiple threads accessing
shared methods. And, quite honestly, I am very confused.

I have tried System.Threading.Monitor.Enter, Synclock, etc., and still
cannot get things to work right. Honestly, it seems to be that for each
shared method, you need to do something to lock it in the 1st line and
unlock it when you exit. Otherwise, anything at anytime can be changed at
any moment by any thread in that method.

For example, if you have a variable loaded with a value at the top of the
method, that you then set a column to that variable while adding a row, you cannot just use System.Threading.Monitor.Enter(DataRowToAdd) to protect the row your are adding, because one of the variables might change by another
thread. You would need to use System.Threading.Monitor.Enter on everything (variables, etc.) adding quite a bit of code. Even at that, I don't see
that System.Threading.Monitor.Enter stops another thread anyway.

I have spend a lot of time one this, and don't understand how to do it. Can you explain to me how to write a shared method that adds rows to a data set, and updates that dataset to the datasource, that can be used by multiple
threads at the same time. Any URLs would be helpf ul to. Maybe if I see a working example, I will grasp what is going on.

Thanks!
Bob Day

Public NotInheritable Class DL
Public Shared Sub tblMETHODS_DETAILSC_ADD_Row(ByVal
tblMethods_Summary_Row_Number_0_Based As Integer, ByVal Summary_Text As
String, Optional ByVal Details_Text As String = "")

' code to add row to table methods_details and update that row to
datasource of same name

end sub

end class

Nov 20 '05 #2

"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:ed**************@TK2MSFTNGP09.phx.gbl...
VS 2003, vb.net , sql msde...

I have an application with multiple threads running. Its a telephony
application where each thread represents a telephone line. For code that
would be the same for each thread, I put in Shared methods as below. It is only now that I am realizing the complexity of multiple threads accessing
shared methods. And, quite honestly, I am very confused.

I have tried System.Threading.Monitor.Enter, Synclock, etc., and still
cannot get things to work right. Honestly, it seems to be that for each
shared method, you need to do something to lock it in the 1st line and
unlock it when you exit. Otherwise, anything at anytime can be changed at
any moment by any thread in that method.

For example, if you have a variable loaded with a value at the top of the
method, that you then set a column to that variable while adding a row, you cannot just use System.Threading.Monitor.Enter(DataRowToAdd) to protect the row your are adding, because one of the variables might change by another
thread. You would need to use System.Threading.Monitor.Enter on everything (variables, etc.) adding quite a bit of code. Even at that, I don't see
that System.Threading.Monitor.Enter stops another thread anyway.

I have spend a lot of time one this, and don't understand how to do it. Can you explain to me how to write a shared method that adds rows to a data set, and updates that dataset to the datasource, that can be used by multiple
threads at the same time. Any URLs would be helpf ul to. Maybe if I see a working example, I will grasp what is going on.

The critical piece you're missing is that local variables don't need to be
protected. Each thread has it's own seperate stack, and the local variables
are on the stack.

Global variables, or Shared variables DO need to be protected. So if your
shared method doesn't reference any global or shared variables, then there's
no need to use any syncronization.

So the question is "what objects are you sharing between threads", not "what
methods". Then you must use appropriate serialization mechanisms to protect
those shared objects.

Think about it, and post a more complete example, and perhaps someone can
suggest a strategy.

David
Nov 20 '05 #3

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:vt************@corp.supernews.com...
Use a MuteX variable and then have your threads enter a wait stait for when the variable is unlocked...


I belive the poster knows _how_to serialize access to a section of code.
The issue is _when_ to do so.

And SyncLock is much easier and lighter-weight than a Mutex.

David
Nov 20 '05 #4
Obviously you missed the part of the post where he said he didn't want to
use Synclock....
"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:vt************@corp.supernews.com...
Use a MuteX variable and then have your threads enter a wait stait for

when
the variable is unlocked...


I belive the poster knows _how_to serialize access to a section of code.
The issue is _when_ to do so.

And SyncLock is much easier and lighter-weight than a Mutex.

David

Nov 20 '05 #5
You cannot put in a "Friend Shared Sub Write" method somethign like:

Friend Shared Sub Write

SyncLock Me

' code

End SyncLock

end sub

because Me is invalid and Synclok expects an expression. Is there an
alternative expression?

IF I try using it to call the above shared, something like:

SyncLock DLsd.Write

End SyncLock

I get an error that the Write does not produce a value.

So, I don't see how to use Synclock except in limited situations where the
method returns a value.

Please advise.

Bob Day

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:vt************@corp.supernews.com...
Use a MuteX variable and then have your threads enter a wait stait for

when
the variable is unlocked...


I belive the poster knows _how_to serialize access to a section of code.
The issue is _when_ to do so.

And SyncLock is much easier and lighter-weight than a Mutex.

David

Nov 20 '05 #6

"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
You cannot put in a "Friend Shared Sub Write" method somethign like:

Friend Shared Sub Write

SyncLock Me

' code

End SyncLock

end sub


If you want to syncronize a shared method, you need a shared lock object.

class foo
private shared syncRoot as new object 'just something to lock
friend shared sub Write
synclock syncRoot
' code
end synclock
end sub
end class
David


Nov 20 '05 #7
Thanks, that was helpful.

Then what is the difference between the following (where
gcCDsd.DS_Methods.Methods_Details is shared DataSet DataTabe).
Montior.Enter works (I have not tried running the program with Synclock).
SyncLock gcCDsd.DS_Methods.Methods_Details

Monitor.Enter(gcCDsd.DS_Methods.Methods_Details)

You can use monitor in a construct like the following that insures the
shared object is unlocked even if your code to modify the shared object
fails. You cannot do this with SyncLock.
try
Monitor.Enter(gcCDsd.DS_Methods.Methods_Details)
' SyncLock gcCDsd.DS_Methods.Methods_Details ***this does not work, End
Synclock cannot be in finally
... code to modify gcCDsd.DS_Methods.Methods_Details
Finally
Monitor.Exit(gcCDsd.DS_Methods.Methods_Details)
' End SyncLock gcCDsd.DS_Methods.Methods_Details ***this does not work,
End Synclock cannot be in finally.
end try

Thanks for all your help.
Bob Day

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:ec**************@TK2MSFTNGP11.phx.gbl...

"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
You cannot put in a "Friend Shared Sub Write" method somethign like:

Friend Shared Sub Write

SyncLock Me

' code

End SyncLock

end sub


If you want to syncronize a shared method, you need a shared lock object.

class foo
private shared syncRoot as new object 'just something to lock
friend shared sub Write
synclock syncRoot
' code
end synclock
end sub
end class
David

Nov 20 '05 #8
That was very helpful, thanks.
Bob Day

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...

"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:ed**************@TK2MSFTNGP09.phx.gbl...
VS 2003, vb.net , sql msde...

I have an application with multiple threads running. Its a telephony
application where each thread represents a telephone line. For code that would be the same for each thread, I put in Shared methods as below. It is
only now that I am realizing the complexity of multiple threads accessing shared methods. And, quite honestly, I am very confused.

I have tried System.Threading.Monitor.Enter, Synclock, etc., and still
cannot get things to work right. Honestly, it seems to be that for each
shared method, you need to do something to lock it in the 1st line and
unlock it when you exit. Otherwise, anything at anytime can be changed at any moment by any thread in that method.

For example, if you have a variable loaded with a value at the top of the method, that you then set a column to that variable while adding a row,

you
cannot just use System.Threading.Monitor.Enter(DataRowToAdd) to protect

the
row your are adding, because one of the variables might change by another thread. You would need to use System.Threading.Monitor.Enter on

everything
(variables, etc.) adding quite a bit of code. Even at that, I don't see
that System.Threading.Monitor.Enter stops another thread anyway.

I have spend a lot of time one this, and don't understand how to do it.

Can
you explain to me how to write a shared method that adds rows to a data

set,
and updates that dataset to the datasource, that can be used by multiple threads at the same time. Any URLs would be helpf ul to. Maybe if I

see a
working example, I will grasp what is going on.
The critical piece you're missing is that local variables don't need to be
protected. Each thread has it's own seperate stack, and the local

variables are on the stack.

Global variables, or Shared variables DO need to be protected. So if your
shared method doesn't reference any global or shared variables, then there's no need to use any syncronization.

So the question is "what objects are you sharing between threads", not "what methods". Then you must use appropriate serialization mechanisms to protect those shared objects.

Think about it, and post a more complete example, and perhaps someone can
suggest a strategy.

David

Nov 20 '05 #9

"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:eU**************@TK2MSFTNGP09.phx.gbl...
Thanks, that was helpful.

Then what is the difference between the following (where
gcCDsd.DS_Methods.Methods_Details is shared DataSet DataTabe).
Montior.Enter works (I have not tried running the program with Synclock).
SyncLock gcCDsd.DS_Methods.Methods_Details

Monitor.Enter(gcCDsd.DS_Methods.Methods_Details)

You can use monitor in a construct like the following that insures the
shared object is unlocked even if your code to modify the shared object
fails. You cannot do this with SyncLock.
try
Monitor.Enter(gcCDsd.DS_Methods.Methods_Details)
' SyncLock gcCDsd.DS_Methods.Methods_Details ***this does not work, End Synclock cannot be in finally
... code to modify gcCDsd.DS_Methods.Methods_Details
Finally
Monitor.Exit(gcCDsd.DS_Methods.Methods_Details)
' End SyncLock gcCDsd.DS_Methods.Methods_Details ***this does not work, End Synclock cannot be in finally.
end try

Thanks for all your help.
Bob Day


SyncLock is a convenience feature that saves you from having to use a
finally block to leave the Monitor.

SyncLock o
'do something
end SyncLock

Is compiled as

Monitor.Enter(o)
try
'do something
finally
Monitor.Exit(o)
end try
So puting "end SyncLock" in a finally block is redundant.

David
Nov 20 '05 #10

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

Similar topics

2
by: Michael Schmitt | last post by:
Hello. What is the usual way for running functions in parallel on a multiple-processor machine. Actually I want to run a single computationally expensive function with different parameter sets....
17
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next...
7
by: Guyon Morée | last post by:
If I have multiple threads reading from the same file, would that be a problem? if yes, how would I solve it? Let's say I want to take it a step further and start writing to 1 file form...
8
by: Angel | last post by:
Hi everybody, I am new to ASP.NET, and my question might be obvious to most of you but I do not seem to find many things about threads and ASP.NET. I have an object(Object 1) which need some...
2
by: Oenone | last post by:
I'm upgrading a DLL from VB6 to VB.NET. The DLL gets called from an ASP.NET web application. In the VB6 code there is a module-level object which stores the context about what the user is doing...
2
by: Jon C | last post by:
Hi, I've written a multi-threaded web spider which stores search results in a Shared ArrayList. Since each thread can launch further threads, the problem I have is pausing my main sub until all...
4
by: Gregory Gadow | last post by:
I've cobbled together a PrinterClass that takes a text file and dumps it to a printer. The app using is has multiple threads, all of which need access to a shared instance. Can someone point me to...
8
by: Flack | last post by:
Hey guys, In my app I have a bitmap where drawing is done and in the form's paint method I show the bitmap: private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {...
19
by: Zytan | last post by:
I want multiple instances of the same .exe to run and share the same data. I know they all can access the same file at the same time, no problem, but I'd like to have this data in RAM, which they...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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,...
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
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,...
0
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...

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.