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

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 4334
"Adam" <ad*********@hotmail.coma écrit dans le message de news:
11*********************@e3g2000cwe.googlegroups.co m...

| 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*********@hotmail.coma écrit dans le message de news:
11*********************@e3g2000cwe.googlegroups.co m...
>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*********@hotmail.coma écrit dans le message de news:
11*********************@e3g2000cwe.googlegroups.co m...
>>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
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...
2
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,...
3
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...
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...
9
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...
2
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...
11
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...
11
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...
3
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...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.