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

Static Class and Synchronization ???

Hi

Are the members in a static class in C# class synchronized for multiple
thread access. If yes, are all static members in a C# class auto
synchronized ?

Regards
Vivek Ragunathan

May 2 '06 #1
2 5459
"Vivek Ragunathan" <Ke*****@gmail.com> wrote:
Are the members in a static class in C# class synchronized for multiple
thread access.
No.
If yes, are all static members in a C# class auto
synchronized ?


No.

-- Barry
May 2 '06 #2
Vivek Ragunathan wrote:
Are the members in a static class in C# class synchronized for multiple
thread access. If yes, are all static members in a C# class auto
synchronized ?


You can use the (System.Runtime.CompilerServices) MethodImpl attribute
to lock static or instance methods as MethodImplOptions.Synchronized.
<excerpt isbn="1590593863"> This may aid clarity a bit by cutting down
the nesting level within the method, but it's generally disparaged for
two rather good reasons:

1. The lock is obtained before entering the body of the method, and is
not released until the method returns. Using explicit locking gives
you finer-grained locking – you only keep the lock open as long as you
absolutely need to. While this isn't really an issue with simple
methods that add or remove an element from some sort of data
structure, these are also not usually the sort of complexly structured
methods where cutting one level of indenting can have any real effect
on clarity.

2. Synchronized methods are still using the Monitor mechanism, but
it's no longer obvious which object is being locked. (Static methods
lock the object's Type; instance methods lock this/Self, the object's
instance.) This can lead to locking failure or even deadlock. A hidden
lock can lead to lock failure if you 1) forget that synchronized
static methods do not lock the same data structure as synchronized
instance methods, if you 2) access the same static data structures
while explicitly locking something besides the class type, or if you
3) access the same data structures from the synchronized methods of a
different class. A hidden lock can lead to deadlock, both because you
are now locking a data structure that may be publicly visible, and
also because somebody maintaining your code may not be aware what a
synchronized method is actually locking.
</excerpt>

Answering you specifically: The members can be synchronized.
Synchronized static methods lock the class's runtime Type, and only
one synchronized static method (per class) may run at a time.
(Synchronized instance methods lock the class's runtime Type, and only
one synchronized instance method (per instance) may run at a time.

Locking is not automatic. You have to explicitly apply the attribute.
But then you do have very coarse-grained (all or none) sort-of unique
access to static or instance fields. Access is only sort-of unique,
because ... well ... locking this or typeof(this) can get you laughed
out of all the better offices.

--

<http://www.midnightbeach.com> Contracting, consulting, training
..NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>
In production - in stores by June
May 2 '06 #3

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

Similar topics

10
by: Jeremie | last post by:
Hi, class A { <..> }; const A& GetA() { static A a;
1
by: Ray Ackley | last post by:
I'm experiencing a threading problem that's really perplexing me. I have a multithreaded application that reads car ECU information that's sent over the serial port by the ECU and received by the...
5
by: blah, blah, blah | last post by:
I'm developing a .Net web application and created many helper classes often using static (shared in VB.Net) methods. Do I need to use the lock (SyncLock) statement in these methods to prevent...
6
by: Richard | last post by:
Hi, I write mostly winform app and often create a Business Class Library for my bus logic. When doing this I often set the method to static so I don't need to instantiate the class. Can I do...
10
by: gerry | last post by:
Is there any way to specify that all methods of a class should be synchronized other than applying the attribute for every method ? Gerry
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
1
by: flowstudioLA | last post by:
I have a template class object that I use as a mesaging queue between threads. I use it as a static object that I initialize like so: foo.h class foo{ static LFQueue<const char*,100lfqMyQueue;...
1
by: Angus | last post by:
Hi all I have a design which models a telephone system. Roughly the design has these two items: device class which models an extension on the telephone system. The device constructor takes...
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...
0
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...
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: 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: 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....
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...

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.