473,624 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Thread safety

Hello

Are these 2 properties (please see below) thead-safe? These properties are
generated in the Resources.cs and Settings.cs files in Visual Studio 2005
(whidbey beta 1).

According to CLI memory model specification
http://dotnet.di.unipi.it/EcmaSpec/P...nI/cont11.html, Section 11.6.8
"It is explicitly not a requirement that a conforming implementation of the
CLI guarantee that all state updates performed within a constructor be
uniformly visible before the constructor completes. CIL generators may
ensure this requirement themselves by inserting appropriate calls to the
memory barrier or volatile write instructions. "

The first property (ResourceManage r) doesn't do any locking at all. Let us
assume that we don't care if 2 instances of the class are created (it will
just be an extra allocation that will be collected shortly, no big deal)
But does the use of the temp variable, prevents the case where the property
can return an uninitialized object if the CLR chooses to perform the
assignment before calling the constructor? Can the CLR also detects that
temp is not used again and perform the assignment directly to _resMgr then
call the constructor?

As for the second property, according to section 4.3 of this document,
http://research.microsoft.com/%7Ebir...eadsCSharp.pdf, this
pattern is not thread safe.

What I want to know is, is this a bug in Visual Studio 2005 beta 1?

Here is the code for the 2 properties:

private static System.Resource s.ResourceManag er _resMgr;
public static System.Resource s.ResourceManag er ResourceManager {
get {
if ((_resMgr == null)) {
System.Resource s.ResourceManag er temp = new
System.Resource s.ResourceManag er("MyTestLib.P roperties.Resou rces",
typeof(Resource s).Assembly);
_resMgr = temp;
}
return _resMgr;
}
}
private static Settings m_Value;
private static object m_SyncObject = new object();
public static Settings Value {
get {
if ((Settings.m_Va lue == null)) {
System.Threadin g.Monitor.Enter (Settings.m_Syn cObject);
if ((Settings.m_Va lue == null)) {
try {
Settings.m_Valu e = new Settings();
}
finally {

System.Threadin g.Monitor.Exit( Settings.m_Sync Object);
}
}
}
return Settings.m_Valu e;
}
}
Best regards,
Sherif
Nov 16 '05 #1
6 8271
Sherif ElMetainy <el************ *@wayout.net.NO SPAM> wrote:
Are these 2 properties (please see below) thead-safe?
No.
These properties are
generated in the Resources.cs and Settings.cs files in Visual Studio 2005
(whidbey beta 1).
Oh dear...

Without marking the field as volatile, double-checked locking doesn't
work.

<snip>
What I want to know is, is this a bug in Visual Studio 2005 beta 1?


It certainly looks like it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hello

Thanks for your reply. I reported this issue as a bug
http://lab.msdn.microsoft.com/Produc...0-9da52f4494bd
I also just noticed that the finally block in the Settings.cs property
should be outside the braces of the inner if condition not inside. One can't
make this bug with a lock statement.

Best regards,
Sherif

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Sherif ElMetainy <el************ *@wayout.net.NO SPAM> wrote:
Are these 2 properties (please see below) thead-safe?


No.
These properties are
generated in the Resources.cs and Settings.cs files in Visual Studio 2005 (whidbey beta 1).


Oh dear...

Without marking the field as volatile, double-checked locking doesn't
work.

<snip>
What I want to know is, is this a bug in Visual Studio 2005 beta 1?


It certainly looks like it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Sherif ElMetainy <el************ *@wayout.net.NO SPAM> wrote:
Thanks for your reply. I reported this issue as a bug
http://lab.msdn.microsoft.com/Produc...0-9da52f4494bd
I also just noticed that the finally block in the Settings.cs property
should be outside the braces of the inner if condition not inside.
Yup.
One can't make this bug with a lock statement.


Indeed - I'm not sure why the template doesn't use a lock. Very odd.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hi Jon,
Sherif ElMetainy <el************ *@wayout.net.NO SPAM> wrote:
Thanks for your reply. I reported this issue as a bug
http://lab.msdn.microsoft.com/Produc...0-9da52f4494bd
I also just noticed that the finally block in the Settings.cs property
should be outside the braces of the inner if condition not inside.

Yup.

One can't make this bug with a lock statement.

Indeed - I'm not sure why the template doesn't use a lock. Very odd.


Well, take a look at your own link :-)

http://www.yoda.arachsys.com/csharp/...winforms.shtml

What is locking good for, when you cannot use multithreading
with WinForms.

bye
Rob
Nov 16 '05 #5
Robert Jordan <ro*****@gmx.ne t> wrote:
Indeed - I'm not sure why the template doesn't use a lock. Very odd.


Well, take a look at your own link :-)

http://www.yoda.arachsys.com/csharp/...winforms.shtml

What is locking good for, when you cannot use multithreading
with WinForms.


Um, you can use multithreading with WinForms. You just need to be
careful.

However, the point was that Monitor.Enter and Monitor.Exit was being
used in the template (and incorrectly at that) rather than the simpler
way, which is to use "lock", which inserts correct calls to
Monitor.Enter and Monitor.Exit.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Robert Jordan <ro*****@gmx.ne t> wrote:
> Indeed - I'm not sure why the template doesn't use a lock. Very odd.
Well, take a look at your own link :-)

http://www.yoda.arachsys.com/csharp/...winforms.shtml

What is locking good for, when you cannot use multithreading
with WinForms.


Um, you can use multithreading with WinForms. You just need to be
careful.

However, the point was that Monitor.Enter and Monitor.Exit was being
used in the template (and incorrectly at that) rather than the simpler
way, which is to use "lock", which inserts correct calls to
Monitor.Enter and Monitor.Exit.


The actual reason is probably that CodeDOM doesn't have any way of
expressing the lock statement, so a code generator is effectivly forced to
write out the correct code. I think settings.cs is generated via CodeDOM,
probably via the same codebase that VB and other langauges use(I know VB's
generated code looks pretyt similar, except for namespace differences).
The feedback center link given earlier in this thread also suggests this. --
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

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

Similar topics

4
6640
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause potential problems for thread-safety. So far, I'm only confused. I need a proper explanation for the concept so I can understand how to write thread-safe functions in the future. My apologies for posting a long routine.
9
2082
by: Alexander Fleck | last post by:
Hi, I' ve to make a software module thread safe. I know how to realize that and what' re the main topics of thread safety. But I don' t know how thread safety can be tested. I read about a test for web servers. These apps' re tested with a stress test. That doesn' t work for my module. I searched the web but didn' t find a solution that satisfies me. I think that thread safety errors don' t occur reproduceable and so they' re hard to test and...
4
2785
by: The Crow | last post by:
for example i have static readonly SqlParameter and i want to clone them at runtime. as clone operation will not write to SqlParameter object, just reading, should i lock that object during read operations?
22
37721
by: Brett | last post by:
I have a second thread, t2, that errors out and will stop. It's status is then "Stopped". I try to start t2 from thread 1, t1, by checking If t2.threadstate = "Stopped" Then t2.start() However, this throws and error: System.Threading.ThreadStateException: Thread is running or terminated; it can not restart.
4
2550
by: Warren Sirota | last post by:
Hi, I've got a method that I want to execute in a multithreaded environment (it's a specialized spider. I want to run a whole bunch of copies at low priority as a service). It works well running as a single application. I was wondering if there is a "Thread-Safety Analysis Wizard"? I'm sure I'm grossly off-base with the following, so I'm prepared to be embarrassed. Please point me in the right direction!
6
3132
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it to the client. Before adding data to the arraylist, I check if the depth of the arraylist is longer than iMaxQueueDepth, and if it is, I clear the arraylist. Is it possible that while I am clearing the arraylist, the ThreadMain at the same time...
1
3623
by: paul.hester | last post by:
Hi all, All of the classes in my DAL are static, with constants defining the stored procedures and parameters. I've been having some problems with my site which makes me wonder if there's a thread safety issue. Are consts thread safe? Would the following example create any thread safety issues? Would you recommend using static readonly members instead of constants?
13
3581
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
0
4121
by: Graham Wideman | last post by:
Folks: Can anyone tell me what controls php's "thread safety" feature? I have an installation where phpinfo() is showing Thread safety: enabled, whereas I need it disabled in order to work with xdebug.so. So far as I can tell, the options I used to configure php did not ask for thread-safety, and I also don't see any options to *dis*able thread-safety. Configure --help does show several threading-related options, but none for
13
11449
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
0
8168
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,...
0
8672
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
8471
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
7153
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...
1
6107
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
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
4075
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...
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1474
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.