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

Using static methods in apotentially multithreaded environment

Hi all,

In my project I have made a number of helper methods static. As I understand
it, this will create the problem that multiple threads could access the
static method at the same time and interfere with one another.

My question is, for each static method, do I need to lock access to only one
call at a time? I've noticed that Microsofts Data Application block also
uses static methods for its data access calls - for example execute and
executeNonQuery etc. Should these methods be changed to only allow access by
a single thread at any given moment?
Perhaps there is an easier solution than this. A colleague of mine is
banging on about what a terrible performance hit this will cause but given
that the application is quite small I dont think that it will be much of a
problem.

Any suggestions on what to do would be very much appreciated.

Thank you all

Simon

Jul 21 '05 #1
9 1953
I don't believe static methods pose any more or any less of a problem than
non-static methods when it comes to threading. You still have to lock any
data in your class when there is the potential for multiple threads to be
accessing the data concurrently.

The overhead can be quite large, but this largely depends on the frequency
of calls, length of time in the call etc.

Please check out Jon Skeet's advice in
http://www.pobox.com/~skeet/csharp/multithreading.html for more information.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Simon Harvey" <sh856531@microsofts_free_email_service.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi all,

In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the
static method at the same time and interfere with one another.

My question is, for each static method, do I need to lock access to only one call at a time? I've noticed that Microsofts Data Application block also
uses static methods for its data access calls - for example execute and
executeNonQuery etc. Should these methods be changed to only allow access by a single thread at any given moment?
Perhaps there is an easier solution than this. A colleague of mine is
banging on about what a terrible performance hit this will cause but given
that the application is quite small I dont think that it will be much of a
problem.

Any suggestions on what to do would be very much appreciated.

Thank you all

Simon

Jul 21 '05 #2
Hi Simon,

In my opinion can when an object is instanced for only one class be no
problem at all with multithreading (when the object not access other static
members) however with static members I would advice you to look to the Jon
Skeet page wherefore John gave you the link already.

Cor


In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the
static method at the same time and interfere with one another.

My question is, for each static method, do I need to lock access to only one call at a time? I've noticed that Microsofts Data Application block also
uses static methods for its data access calls - for example execute and
executeNonQuery etc. Should these methods be changed to only allow access by a single thread at any given moment?
Perhaps there is an easier solution than this. A colleague of mine is
banging on about what a terrible performance hit this will cause but given
that the application is quite small I dont think that it will be much of a
problem.

Any suggestions on what to do would be very much appreciated.

Thank you all

Simon

Jul 21 '05 #3
threading only becomes a problem if there's shared state. if you don't have any shared state, then it's thread safe.

"Simon Harvey" wrote:
Hi all,

In my project I have made a number of helper methods static. As I understand
it, this will create the problem that multiple threads could access the
static method at the same time and interfere with one another.

My question is, for each static method, do I need to lock access to only one
call at a time? I've noticed that Microsofts Data Application block also
uses static methods for its data access calls - for example execute and
executeNonQuery etc. Should these methods be changed to only allow access by
a single thread at any given moment?
Perhaps there is an easier solution than this. A colleague of mine is
banging on about what a terrible performance hit this will cause but given
that the application is quite small I dont think that it will be much of a
problem.

Any suggestions on what to do would be very much appreciated.

Thank you all

Simon

Jul 21 '05 #4
That is true. If no shared state, then don't need locking. If state set in
constructor and read only, then don't need locking. However, if shared rw
state, then you have to lock as normal - static does not change this
requirement. If most of your threads will be reads, then you may look into
a readerwriter lock instead of a monitor. However a rw lock is slower then
monitor, so you will have determine perf benefit to your app via testing.
Things like blocking, waits, etc can effect all this. If each thread only
grabs lock for short time without any waiting inside the lock, then Monitor
may be a better way to go. If your just updating and reading counters then
using Interlocked class instead of monitor could be used. However many
times you need to treat sets of vars atomically so they are consistent at
any give time in respect to other threads. Moreover, you also need to
determine if using multiple threads is actually going to increase
performance or decrease it. You may be able to refactor your model to avoid
the locks and get better perf with one thread. Or use a producer consumer
with shared queue in the middle. One thread produces at full blast and one
or more consumers can read objects as available. If your consumers will
block on same io anyway (such as a socket or file), one consumer may perform
better then many.

--
William Stacey, MVP

"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
threading only becomes a problem if there's shared state. if you don't have any shared state, then it's thread safe.
"Simon Harvey" wrote:
Hi all,

In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the
static method at the same time and interfere with one another.

My question is, for each static method, do I need to lock access to only one call at a time? I've noticed that Microsofts Data Application block also
uses static methods for its data access calls - for example execute and
executeNonQuery etc. Should these methods be changed to only allow access by a single thread at any given moment?
Perhaps there is an easier solution than this. A colleague of mine is
banging on about what a terrible performance hit this will cause but given that the application is quite small I dont think that it will be much of a problem.

Any suggestions on what to do would be very much appreciated.

Thank you all

Simon


Jul 21 '05 #5
Hi All,

With regards to shared data, I have a Connection object and a command object
that is declared and used inside the static method. It is completely local.

Is this considered shared? I mean, is it shared by threads at the same time?

Also, is there a way to lock on a method rather than on a particular object.
Basically so you know that only one thread is in a method at a given moment?

Thanks for your help

Simon
Jul 21 '05 #6
Hi Simon,
With regards to shared data, I have a Connection object and a command object that is declared and used inside the static method. It is completely local.
Is this considered shared? I mean, is it shared by threads at the same time?
Also, is there a way to lock on a method rather than on a particular object. Basically so you know that only one thread is in a method at a given

moment?

I think that it is much harder you can only use a command on a connection
for one process in a time (Net 1.1). So every thread has to wait untill the
other one is completly finished. So I think it is not wise to use a shared
method here.

However just my thought, and to point you on this problem.

Cor
Jul 21 '05 #7
Local variables are created on the stack in the context of the calling
thread and are usually completely isolated from other threads calling the
same function.

You can lock on a method by having a sync object available that is only used
by that function. Just declare it as a static member of the class (even a
new object() would suffice).

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Simon Harvey" <sh856531@microsofts_free_email_service.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi All,

With regards to shared data, I have a Connection object and a command object that is declared and used inside the static method. It is completely local.
Is this considered shared? I mean, is it shared by threads at the same time?
Also, is there a way to lock on a method rather than on a particular object. Basically so you know that only one thread is in a method at a given moment?
Thanks for your help

Simon

Jul 21 '05 #8
"Simon Harvey" wrote:
Hi All,

With regards to shared data, I have a Connection object and a command object
that is declared and used inside the static method. It is completely local.


that is not shared. it's perfectly thread safe if they are all local.
Jul 21 '05 #9
Yes local connection and command objects should not be a problem.

Just as a note, you can use an attribute to lock a method without explicitly
creating a sync root object etc.
[System.Runtime.CompilerServices.MethodImpl(MethodI mplOptions.Synchronized)]
public static void MyThreadedMethod()
{
// ...
}

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"John Wood" <j@ro.com> wrote in message
news:e6**************@tk2msftngp13.phx.gbl...
Local variables are created on the stack in the context of the calling
thread and are usually completely isolated from other threads calling the
same function.

You can lock on a method by having a sync object available that is only used by that function. Just declare it as a static member of the class (even a
new object() would suffice).

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Simon Harvey" <sh856531@microsofts_free_email_service.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi All,

With regards to shared data, I have a Connection object and a command

object
that is declared and used inside the static method. It is completely

local.

Is this considered shared? I mean, is it shared by threads at the same

time?

Also, is there a way to lock on a method rather than on a particular

object.
Basically so you know that only one thread is in a method at a given

moment?

Thanks for your help

Simon


Jul 21 '05 #10

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

Similar topics

2
by: Tryion | last post by:
Hi, I'd like to know if it's possible/responsible to use the ReaderWriterLock class (RWL) in a class without declaring it as "static". The example in the SDK does not use a static RWL. However,...
9
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
3
by: Adam | last post by:
What happens if one thread is executing a static constructor and another thread starts. Does the second thread block until the first is done in the static constructor? I want to make sure all my...
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
6
by: semkaa | last post by:
Can you explain why using ref keyword for passing parameters works slower that passing parameters by values itself. I wrote 2 examples to test it: //using ref static void Main(string args) {...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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 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.