473,701 Members | 2,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Class and Synchronization ???


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 ?

Vivek Ragunathan

May 2 '06 #1
2 5481
"Vivek Ragunathan" <Ke*****@gmail. com> 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 ?


-- 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.

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

by: Jeremie | last post by:
Hi, class A { <..> }; const A& GetA() { static A a;
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 -
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...
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,
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
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 & );
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."
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;
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
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...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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,...
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...
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();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.