473,807 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5486
"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 .CompilerServic es) MethodImpl attribute
to lock static or instance methods as MethodImplOptio ns.Synchronized .
<excerpt isbn="159059386 3"> 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.midnightbea ch.com> Contracting, consulting, training
..NET 2.0 for Delphi Programmers <http://www.midnightbea ch.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
1931
by: Jeremie | last post by:
Hi, class A { <..> }; const A& GetA() { static A a;
1
1698
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 laptop/program. I'll try to give a concise synopsis as the program is easily 10k+ lines. main.cs - contains the application start function. Here's the entire code (minus misc junk) as it's rather small -
5
1961
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 multiple web application threads from using the same method at the same time? For example, I have a static method that extracts information from a page request cookie for use by other parts of the application. Since this happens on every request I...
6
1758
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 the same with ASP.Net or will all user end up getting the same copy for the business object? Thanks,
10
3295
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
3885
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 variables, just static members static void FirstFunction( args & ); static void SecondFunction( args & ); static void ThirdFunction( args & );
2
3778
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 common meanings: static as in "statically allocated" as opposed to on the stack or on the free store and static as in "with restricted visibility" as opposed to with external linkage. For member functions, static has the second meaning."
1
3389
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; }; foo.cpp LFQueue<const char*,100Foo::lfqMyQueue;
1
2263
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 a extension number. So program starts by creating a device object for each extension on the telephone system. Each device object contains a static list of calls. Calls are modelled by a
0
9720
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9599
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10374
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10112
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9193
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.