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;
}
}
Is the variable m_floater thread safe? If I write a test program and
run it in VS2005, the m_floater variable ends up in the locals window
in my debugger (for each thread) so it would seem that it is thread
safe. I am assuming this would change if I made my member variable
into somthing that was stored on the managed heap? (i.e. each thread
has its own stack but there is only one heap available for storage).
thanks for helping with this seemingly simple question.
HC
Aug 13 '08
13 11477 He************* @googlemail.com wrote:
On Aug 13, 8:18 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
>Peter Duniho wrote:
>>On Wed, 13 Aug 2008 16:49:41 -0700, <Henri.Chinas.. .@googlemail.co mwrote: 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; } } Is the variable m_floater thread safe? No, not at all. What about that code makes you think it would be?
Data are never thread safe in themselves. Data combined with how they are used can be thread safe or not.
Actually the above code is thread safe.
It is also useless, so the benefits are not that big.
I read Peter's response, makes great sense, thanks. So why then, do
you state that the above code is thread safe? Have I missed something
else?
The code you posted is thread safe.
When you add all the real code, then it may be:
thread safe
not thread safe
If we introduce the volatile aspect it may be:
thread safe without volatile
not thread safe without volatile but thread safe with volatile
not thread safe with volatile
It depends on your code.
Arne
Peter Duniho wrote:
On Wed, 13 Aug 2008 22:24:33 -0700, <He************ *@googlemail.co mwrote:
>[...]
>>Actually the above code is thread safe.
It is also useless, so the benefits are not that big.
I read Peter's response, makes great sense, thanks. So why then, do you state that the above code is thread safe? Have I missed something else?
Arne can be enigmatic sometimes. I suspect he does that intentionally.
Look at your class carefully. If we assume that that's _all_ of your
class, then there's nothing in it that could possibly be affected by
threading. No code in the class ever examines the "m_floater" variable
and it's private which means no code outside the class ever examines it
either. So it doesn't really matter if and when it gets assigned some
new value.
Personally, I figured that for the sake of discussion it made sense to
assume that there's more to your class than just what you posted.
Degenerate cases are mostly useful as base cases for induction, not
general conversation. :)
But clearly Arne felt otherwise, and took your code sample literally.
And yes, taken exactly as-is, there's no hazard using the class you
posted in a multi-threaded environment. In that sense, it's completely
thread-safe. :)
Since it is impossible to guess whether the code not posted is thread
safe or not, then ...
Arne
Peter Duniho wrote:
Not true. First, if the variable isn't marked "volatile" (and it's not
in the example code we're talking about) it's still not thread-safe
because the compiler is allowed to generate code that could cache the
value somewhere invisible to some other thread.
You can not conclude that.
There are other ways of ensuring visibility between threads than
volatile.
Arne
On Thu, 14 Aug 2008 19:16:58 -0700, Arne Vajhøj <ar**@vajhoej.d kwrote:
Peter Duniho wrote:
>Not true. First, if the variable isn't marked "volatile" (and it's not in the example code we're talking about) it's still not thread-safe because the compiler is allowed to generate code that could cache the value somewhere invisible to some other thread.
You can not conclude that.
There are other ways of ensuring visibility between threads than
volatile.
That's true. But none of those other memory barriers exist in the code
that was posted. The "volatile" keyword would be the simplest way to
address that. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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
|
by: |
last post by:
I find in the documentation the following
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.
Then, I go to the members documentation and I find public methods or
|
by: Dennis |
last post by:
I have a question about thread safety in a VB application written using Visual
Studio.Net 2003.
Here is the situation...
I am running a process thread in a modal dialog. I have written it so the user
cannot exit the dialog until the thread completes (unless they click a button
to explicitly interrupt and destroy the thread).
My question...
|
by: Chad Zalkin |
last post by:
We are evaluating some old code that was written as part of our math
library.
This code uses some optimizations that I'm not sure are necessary or
safe, but is a source of debate between my coworkers.
Method 1 includes a temporary storage varriable at class scope.
Method 2 includes a temporary storage varriable at method scope.
Method 3 includes a temporary static storage varriable at method scope.
Are any of the methods better than...
| |
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!
|
by: Laser Lu |
last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN.
The declaration is usually described as 'Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.'
So, does this mean All the static/shared methods written in .NET compatible programming language, such as C#, VB.NET, are guaranteed to be...
|
by: cgwalters |
last post by:
Hi,
I've recently been working on an application which does quite a bit
of searching through large data structures and string matching, and I
was thinking that it would help to put some of this CPU-intensive work
in another thread, but of course this won't work because of Python's
GIL.
There's a lot of past discussion on this, and I want to bring it up
again because with the work on Python 3000, I think it is worth trying
|
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: 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: 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,...
| |
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: 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: 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...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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.
| |