473,408 Members | 2,032 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,408 software developers and data experts.

static member method

Hi,

I have the following class:
class MyClass
{

public static void SomeMethod(string str)
{
string local = "AAA";
string test = local + str;
.....
}

}

This public static method can be used by multiple threads. Is it needed to
lock the local variable test, or every thread will have its own copy of this
variable as it is local?

Thanks,
Lubomir
Apr 27 '07 #1
3 2218
On Apr 27, 10:20 am, Lubomir <Lubo...@discussions.microsoft.com>
wrote:
Hi,

I have the following class:
class MyClass
{

public static void SomeMethod(string str)
{
string local = "AAA";
string test = local + str;
.....
}

}

This public static method can be used by multiple threads. Is it needed to
lock the local variable test, or every thread will have its own copy of this
variable as it is local?
Every thread will have its own copy on the stack.

Data is shared across threads only if the data item itself is marked
"static".

Apr 27 '07 #2
Thanks.

"Bruce Wood" wrote:
On Apr 27, 10:20 am, Lubomir <Lubo...@discussions.microsoft.com>
wrote:
Hi,

I have the following class:
class MyClass
{

public static void SomeMethod(string str)
{
string local = "AAA";
string test = local + str;
.....
}

}

This public static method can be used by multiple threads. Is it needed to
lock the local variable test, or every thread will have its own copy of this
variable as it is local?

Every thread will have its own copy on the stack.

Data is shared across threads only if the data item itself is marked
"static".

Apr 27 '07 #3
On Apr 27, 10:40 am, Bruce Wood <brucew...@canada.comwrote:
On Apr 27, 10:20 am, Lubomir <Lubo...@discussions.microsoft.com>
wrote:


Hi,
I have the following class:
class MyClass
{
public static void SomeMethod(string str)
{
string local = "AAA";
string test = local + str;
.....
}
}
This public static method can be used by multiple threads. Is it needed to
lock the local variable test, or every thread will have its own copy of this
variable as it is local?

Every thread will have its own copy on the stack.

Data is shared across threads only if the data item itself is marked
"static".
Sorry. That was inaccurate. Teach me to type fast and think later.

Data will be shared across threads only if the item is declared as a
field in the object, regardless of whether it's a static or instance
member.

Local variables within methods are stored on the stack and so there is
one per thread.

So... you're still OK. It's just that my explanation of _why_ your
code is OK was wrong.

Apr 28 '07 #4

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

Similar topics

4
by: raghavendra | last post by:
Hi , A static member can be accessed only by another static method....but the vice-versa is not true....Can anyone pls explain me the logic behind this... Also in a project, if we have too many...
7
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static...
7
by: Chris Clement | last post by:
I have been handed a project that someone else started and most of it was developed in the VS.NET design mode. For whatever reasons, when I try to make changes to the controls in VS.NET design...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
5
by: SemSem | last post by:
when i was usong c++ idont have to put static before tha function that i want to acces but in c# i have to but it . ex : ihave a function called add(int x,int y) i have to put static int...
5
by: TomislaW | last post by:
What is the purpose or difference between private static and private method in non-static class?
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...
5
by: Doru Roman | last post by:
Hi, Can somebody explain please the meaning and use of a STATIC method? Thanks, Doru
9
by: sonu | last post by:
Hi All, Pls clarify me what is the difference between static member and static method in c. pls some one reply me with Example.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.