473,651 Members | 3,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static constructor multithreaded?

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 globals are
initialized and the second thread does not throw an exception.

Thanks in advance.

Adam Smith

Oct 8 '06 #1
3 4353
"Adam" <ad*********@ho tmail.coma écrit dans le message de news:
11************* ********@e3g200 0c...legrou ps.com...

| 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 globals are
| initialized and the second thread does not throw an exception.

Static constructors are only ever called once, and static methods are
implicitly threadsafe, so you shouldn't have to take any special
precautions.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Oct 8 '06 #2
Joanna Carter [TeamB] wrote:
"Adam" <ad*********@ho tmail.coma écrit dans le message de news:
11************* ********@e3g200 0c...legrou ps.com...
>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
globals are initialized and the second thread does not throw an
exception.

Static constructors are only ever called once, and static methods are
implicitly threadsafe, so you shouldn't have to take any special
precautions.
Static constructors are only called once, but static methods are most
certainly NOT implicitly threadsafe! If a static method modifies data (e.g.
static fields), then it needs the same thread safety considerations that a
non-static method would need. Particularly, synchronization around access
to the shared state.

-cd
Oct 8 '06 #3
Carl Daniel [VC++ MVP] wrote:
Joanna Carter [TeamB] wrote:
>"Adam" <ad*********@ho tmail.coma écrit dans le message de news:
11************* ********@e3g200 0c...legrou ps.com...
>>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
globals are initialized and the second thread does not throw an
exception.
Static constructors are only ever called once, and static methods are
implicitly threadsafe, so you shouldn't have to take any special
precautions.

Static constructors are only called once, but static methods are most
certainly NOT implicitly threadsafe! If a static method modifies data (e.g.
static fields), then it needs the same thread safety considerations that a
non-static method would need. Particularly, synchronization around access
to the shared state.
Should we guess that "...and static methods are..." should
have been "...and static constructors are...", because that
makes most sense in the context.

Arne
Oct 8 '06 #4

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

Similar topics

9
1973
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 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...
2
1720
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, the documentation has the standard disclaimer of "Any public static members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe." And the members of this class are not static, so all of...
3
339
by: Kevin C | last post by:
Will object instantiation of an object not occur until the static constructor has run? In other words, if I have logic in a static constructor that takes, lets say 5 seconds. Will all other attempts to create an object of that type be "blocked" for that 5 secs until the static constructor processing has occurred?
1
1682
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 -
9
4154
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the verbaige completely wrong, but here goes nothing ... I'm currently using the MS Data Access Block for a desktop application I'm writing. Recently, I had to add a call to a web service, which in
2
2653
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the following type : Exception, Error, Warning, Infos (with differents colors, etc). I would like to use it like the Console.WriteLine("My Message"); on everywhere in my application.
11
2239
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 multithreaded operations. Any instance members are not guaranteed to be thread safe." I have 2 questions: 1. I thought dynamic variables are thread-safe since threads have their own
11
3815
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
3
5106
by: Charles Nicholson | last post by:
Hello all- I have some static C++ libraries that I wrote in VS2003 but which upgraded fine when i went to VS2005 Pro. In them i overload the global versions of operators new, new, delete, and delete. I also use the STL and parts of boost (shared_ptr<> and weak_ptr<>) pretty heavily. They have dependencies on various windows libs (dbghelp, winsock, etc...). These static libraries have no common runtime support at all and use the...
0
8361
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
8807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
jinu1996
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...
1
8466
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
7299
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
4144
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...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
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
2
1588
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.