473,326 Members | 2,168 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,326 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

Nov 16 '05 #1
9 12196
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

Nov 16 '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

Nov 16 '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

Nov 16 '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


Nov 16 '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
Nov 16 '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
Nov 16 '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

Nov 16 '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.
Nov 16 '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


Nov 16 '05 #10

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

Similar topics

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...
8
by: Steven Livingstone | last post by:
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods? I understand the C# CLS states this, but just interested in the reasons behind...
4
by: Joe Fallon | last post by:
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of such are (e.g. locking static variables when...
5
by: chandu | last post by:
can any one give advantages and disadvantages to using static methods in a class. shall we use complete static methods in a class ? thanks
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.